"Java持久性与Hibernate,第二版"一书中的用例 [英] use case in the book "Java Persistence with Hibernate, 2nd Edition"

查看:67
本文介绍了"Java持久性与Hibernate,第二版"一书中的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读《 Java持久性与Hibernate,第二版》一书.它以出售给eBay等竞标者的商品为例.在第400页,作者说

I am reading the book "Java Persistence with Hibernate, 2nd Edition". It takes an example of items sold to bidders like ebay. At the page 400, the author says

查询select i from item i left join fetch i.bids b where b.amount > 20无效.您不能说:加载Item实例并初始化其出价集合,但只能使用具有一定数量的Bid实例."

The query select i from item i left join fetch i.bids b where b.amount > 20 is invalid. You can’t say, "Load the Item instances and initialize their bids collections, but only with Bid instances that have a certain amount."

我不确定是否很了解.我认为这是有效的,但它将仅呈现出价大于20的项目.在这种情况下,它将呈现具有该条件的内部联接

I am not sure to quite understand. To my opinion it is valid, but it will renders only the items that have a bid which amount is greater than 20. It will render in that case an inner join with that condition

此外,以下请求select i from item i left join fetch i.bids b on b.amount > 20是否有效?

Furthermore is the following request select i from item i left join fetch i.bids b on b.amount > 20 valid?

推荐答案

select i from item i left join fetch i.bids b where b.amount > 20,因为HQL无效.因为HQL结果用于返回 Item 对象,所以您不能告诉休眠状态请创建Item对象,但是在创建它时,请勿使用 all s,但仅填充数量大于20的Bid s.

select i from item i left join fetch i.bids b where b.amount > 20 as HQL is invalid. Because the HQL results is used to return Item objects, you cannot tell hibernate to please create Item objects but when you create it, do not populate it with all Bids but populate only with Bids with amount greater than 20.

考虑以下课程

public class Item {
    String name;
    Collection<Bid> bids;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<Bid> getBids() {
        return bids;
    }

    public void setBids(Collection<Bid> bids) {
        this.bids = bids;
    }
}

更新:(让我们来看一个例子,看看如果休眠允许该查询会发生什么)

休眠是否允许发生的主要问题:

Major issues if hibernate had allowed it to happen:

  1. 第一个问题在概念上是错误的,因为使用从仓库中返回的使用item1 java对象的任何服务都不知道item.getBids仅包含金额为> 20
  2. Hibernate仅保留一个对象引用来表示会话中的数据库实体(可重复读取保证,脏检查等).因此,如果您执行另一个Select i from Item i where i.itemId = 1 ,则休眠会出现问题,因为它现在必须具有两个java对象来表示该会话中的项目1(一个出价大于20的对象,另一个出价对item1的所有出价的对象)
  1. First issue this will be conceptually wrong because any service using the item1 java object that was returned from the repo does not know item.getBids only contains bid with amount > 20
  2. Hibernate only keeps one object reference to represent a database entity in a session (Repeatable Read guarantee, dirty checking etc). So if you do another Select i from Item i where i.itemId = 1 , hibernate has a problem because now it has to have two java objects to represent item 1 in that session (one with bids where the amount > 20 and another with all bids of item1)

希望这可以清楚地解释为什么不允许查询

Hope this explains clearly why the query can't be allowed

这篇关于"Java持久性与Hibernate,第二版"一书中的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆