OneToOne Lazy 总是获取子项
我有以下 @OneToOne
关系:
@Entity
@Table(name="USER")
public class User implements Serializable{
private Basket basket;
@OneToOne(cascade = CascadeType.ALL,orphanRemoval=true,mappedBy="user", fetch=FetchType.LAZY )
public Basket getBasket() {
return basket;
}
public void setBasket(Basket basket) {
this.basket = basket;
}
//all other proerties are ommited and none relevant.
}
现在是篮子类:
@Entity
@Table(name="BASKET")
public class Basket implements Serializable {
private User user;
@OneToOne(fetch=FetchType.LAZY)
public User getUser() {
return user;
}
//all other proerties are ommited and none relevant.
}
现在我正在尝试使用 HQL 在具有使用 AOP 的 Spring 事务管理器的对象中获取用户对象:
public User getUser(String param1,String param2) {
Session session = this.sessionfactory.getCurrentSession();
String queryString = "from objects.User user where user.param1=:param1 and user.param2=:param2";
Query query = session.createQuery(queryString);
query.setString("param1", param1);
query.setString("param2", param2);
User user = (User) query.uniqueResult();
return user;
}
但我看到篮子也被获取了它很懒:
Hibernate: select user0......
Hibernate: select basket0.....
为什么?
I have the following @OneToOne
relation:
@Entity
@Table(name="USER")
public class User implements Serializable{
private Basket basket;
@OneToOne(cascade = CascadeType.ALL,orphanRemoval=true,mappedBy="user", fetch=FetchType.LAZY )
public Basket getBasket() {
return basket;
}
public void setBasket(Basket basket) {
this.basket = basket;
}
//all other proerties are ommited and none relevant.
}
Now the basket class:
@Entity
@Table(name="BASKET")
public class Basket implements Serializable {
private User user;
@OneToOne(fetch=FetchType.LAZY)
public User getUser() {
return user;
}
//all other proerties are ommited and none relevant.
}
Now I'm trying to fetch User object using HQL in object which has Spring Transaction manager using AOP:
public User getUser(String param1,String param2) {
Session session = this.sessionfactory.getCurrentSession();
String queryString = "from objects.User user where user.param1=:param1 and user.param2=:param2";
Query query = session.createQuery(queryString);
query.setString("param1", param1);
query.setString("param2", param2);
User user = (User) query.uniqueResult();
return user;
}
but I see that Basket is also fetched although it's Lazy:
Hibernate: select user0......
Hibernate: select basket0.....
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为一对一通常表示高度内聚的对象,并且默认情况下使用联接来获取,所以没有理由不同时获取用户和购物篮。我不确定为什么您会看到两个单独的选择。我相信只有当您专门告诉它使用选择而不是连接来获取时才会发生这种情况。 这篇文章做得很好分析一对一关系。它可能会帮助你。
Because a one-to-one normally represents highly cohesive objects and is fetched using a join by default, so there's little reason not to fetch both user and basket. I'm not sure why you're seeing two separate selects. I believe that should only happen if you tell it specifically to fetch using a select instead of a join. This article does a good job of analyzing a one-to-one relationship. It might help you out.