简体   繁体   English

休眠状态:左联接中的where子句无法正常工作

[英]Hibernate: where clause in left join is not working properly

I have 2 classes in a one-to-many relationship: 我在一对多关系中有2个班级:

   Class Competition{
        @OneToMany(fetch=FetchType.EAGER, mappedBy="competition")   
        public Set<Event> getEvents() {
            return events;
        }
    }
    Class Event{
        @ManyToOne
        @JoinColumn(name="id_competition")
        public Competition getCompetition() {
            return competition;
        }
    }

When I run the query below I receive more data then it should. 当我在下面运行查询时,我会收到更多数据。 In the database, I have a competition entry, lets call it c1, and 3 events: e1, e2 and e3. 在数据库中,我有一个比赛条目,我们将其称为c1,以及3个事件:e1,e2和e3。 One of the events does not meet the condition (e1 has the stardate < '2013-09-13') but when I run the query I get 3 events instead of only 2. I am sure about the data in the database, so what's the problem with the where clause ? 其中一个事件不满足条件(e1的星号<'2013-09-13'),但是当我运行查询时,我得到3个事件,而不是2个。我确定数据库中的数据是肯定的,所以where子句的问题?

"select com from Competition as com inner join com.events event where event.expectedStartdate > '2013-09-13' "

Also, I must specify that if I delete the 2 events that meet the condition and keep the one that does not meet the condition, the query returns nothing (no object Competition). 另外,我必须指定,如果删除两个符合条件的事件并保留不符合条件的事件,则查询将不返回任何内容(无对象竞争)。

I also tried the with clause but I get the same result. 我也尝试了with子句,但得到的结果相同。

When you query for an entity you are always going to receive the fully realized object mapping back. 查询实体时,您总是会收到完全实现的对象映射。 You cannot filter out what goes into the set of events by putting conditions on the query like that. 您不能通过在查询中设置条件来过滤掉事件集中的内容。 If the query finds a Competition then you get the entire competition. 如果查询找到Competition那么您将获得整个竞赛。 The Competition is always going to have all of its children in there when you access the Set. 当您进入比赛时,比赛总是将所有孩子都放在那里。

Put another way that ORM query translates to plain language as: 提出另一种将ORM查询翻译为简单语言的方式:

"Find me every competition that has at least one event that starts after the 13th" “为我找到至少在13日之后开始的至少一项赛事的每项比赛”

not

"Find me every competition and only include events if they start after the 13th." “找到我的每场比赛,只有在13日之后才开始的比赛。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM