简体   繁体   中英

Why does my HQL ignore Where clause

I have this code:

IList<Foo1> ou = session.CreateQuery("from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.foo3.year ='2004'").List<Foo1>();

It returns all objects from Foo1 and ignores where clause. Why? Where is probably mistake?

Next experiments:

 (1) from Foo2 as f2 where f2.year = '2004'  //is OK
 (2) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1)  //is OK
 (3) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.year = '2004'  //returns the same result as (2)
  1. Use SQL profiler to see whether the SQL that gets sent to the server is correct.
  2. Simplify the query until it works. Then work backwards to find the error. Remove the join fetch to see if it changes something.

I had the same problem and it is little bit confusing for me as well, but in one example I found this note: "You don't need the WHERE clause as Hibernate "knows" how to match primary and foreign keys based on the mapping." This "Where" was related to JOIN condition so just try to remove your USING() part.

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