Just getting the value of the id column that does not use union in a hibernate object from one to a large relation

I am using hibernate 4+.

I have two example tables.

Table a

public class A {
  @Id
  private int id;

  @OneToMany(fetch=LAZY)
  private List<B> list;

  // skip getter&setter
}

Table B

public class B {
  @Id
  private int id;

  @ManyToOne(fetch=LAZY)
  @JoinColumn(name="b_id")
  private A a;

  // skip getter&setter
}

Table A (1) - (n) Table B Relation

Is it possible to simply get the identifier in object B not using a connection?

like int help = b.getA (). getId (); // b is an instance of B;

Although I can use the int value instead of A when I declare a class B. But another service level uses A with join.

Is it possible to simply get the id (fk) value?

Please, help.

+3
source share
3 answers

, - . - A , :

@Entity
public class A {
  @Id
  @Access(AccessType.PROPERTY)
  private int id;

  @OneToMany(fetch=LAZY)
  private List<B> list;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

id:

b.getA().getId();

, , , Hibernate getId() ( - ).

+7

B, , - :

  @ManyToOne(fetch=LAZY)
  @JoinColumn(name="b_id")
  private A a;

  @Column(name="b_id", updatable=false,insertable=false) //Or correct column
  private int a_id;

, A B.

+2

I think you can use this post: property access strategy

They say that lazy loading will not start if you get access only to id

+2
source

All Articles