简体   繁体   中英

Hibernate query for joining two tables

I have two tables Employee and User. I need to join these tables like,

"select userName,firstName from Employee left join User on Employee.userId = User.userId"

How can I do it in Hibernate-spring.

You cannot express arbitrary joins on any two columns in JPQL, effectively there is no 'on.' You need to express the relationship between the two entities as an object relationship (map one on the other) then you just write Employees e left join Users u .

I think that's as much as can be said without seeing your entities.

It's not clear from your description the way you are accessing the the data, meaning using criteria query, HQL or using the mappings. But in general for forcing join in resultant SQL query, you need to mention explicit configuration in mappings and criteria queries. Checkout this for joining tables. This refers to Hibernate 3.x not sure what version you are using. This link gives concise version of fetching strategies.

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