OneToOne Lazy 总是获取子项

发布于 2024-11-19 23:27:06 字数 1357 浏览 2 评论 0原文

我有以下 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-11-26 23:27:06

因为一对一通常表示高度内聚的对象,并且默认情况下使用联接来获取,所以没有理由同时获取用户和购物篮。我不确定为什么您会看到两个单独的选择。我相信只有当您专门告诉它使用选择而不是连接来获取时才会发生这种情况。 这篇文章做得很好分析一对一关系。它可能会帮助你。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文