[英]Get JoinColumn id value without query in Hibernate
@Entity
@Table(name="A")
public class A {
private int id;
//REST OF FIELDS...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_ID", nullable = true)
private B b;
//GETTERS AND SETTERS...
}
@Entity
@Table(name="B")
public class B {
private int id;
//REST OF FIELDS...
//GETTERS AND SETTERS...
}
When I want to get id of B class object related to A class object 当我想获取与A类对象有关的B类对象的ID时
//...GETTING A OBJECT FROM DATABASE
B b = a.getB();
if(b != null) {
int bId = b.getId();
}
Hibernate makes query to database and gets whole B object but i need id only that is in B_ID column in A table. Hibernate对数据库进行查询并获取整个B对象,但是我只需要A表中B_ID列中的ID。
Is there any way to get B_ID without making additional query ? 有没有办法获得B_ID而无需进行其他查询?
If you add 如果添加
@Column(name = "B_ID", insertable = false, updatable = false)
private int b_id;
and a getter 和吸气剂
public int getBId(){
return b_id;
}
Something similar worked for me at least :) 类似的东西至少对我有用:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.