简体   繁体   中英

Hibernate - Double left join to same table

I'm having two tables

First plan_time

NAME | START_FK | END_FK 
------------------------
test | 1        | 2

start_fk and end_fk are FK refering to table times :

ID | NAME
---------
1  | time1
2  | time2 

I'm executing simple select query:

SELECT pt.*, t1.*, t2.* FROM plan_table pt LEFT JOIN times t1 ON t1.id = pt.start_fk LEFT JOIN times t2 ON t2.id = pt.end_fk

And here is the problem. Under sql console I'm getting good result:

First row from plan_table join by first row from times table and second row from times table.

When I'm doing that via hibernate

SQLQuery sqlQuery = s.createSQLQuery( SQL-QUERY )
            .addEntity(PlanTime.class)
            .addEntity(Times.class)
            .addEntity(Times.class);

I'm getting 3 objects

  1. first row from plan_table
  2. first row from times table
  3. first row from times table - this is duplication of second object.

It have to be something with Hibernate... I checked SQL which is send to DB and it is ok.

:/

Please help.

I found workaround for this problem. Here are tips which helped me:

  1. I'm receiving t1 and t2 form PlanTable object via getStartFk() and getEndFk() methods.
  2. I'm using eager fetching

I'm still trying to find out what is status of this issue.

https://hibernate.atlassian.net/browse/HHH-3988

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