简体   繁体   中英

Hibernate: where clause in left join is not working properly

I have 2 classes in a one-to-many relationship:

   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. 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 ?

"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.

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. 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:

"Find me every competition that has at least one event that starts after the 13th"

not

"Find me every competition and only include events if they start after the 13th."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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