I\'m using hibernate 4+.
I have two sample tables.
Table A
public class A {
@Id
private int id;
@OneToMany(fetch=LAZY)
private List&
Yes, because proxies contain the id anyway. To get the id of an A
proxy without initializing it, first declare the id to be accessed via property:
@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;
}
}
Then, just read the id:
b.getA().getId();
Changing access type for the id is necessary because if you use field access, Hibernate does not distinguish getId()
method from other ordinary methods (which trigger proxy initialization when invoked).
I think you can use this post : property access strategy
It is said that the lazy loading will not be triggered if you only access id
You can define two fields in B, one representing the relationship, and the other just the column:
@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;
This way you can access entity A or just A's id from entity B.