我想首先说我想认为我非常精通 sql(10 年的经验)并且遇到了头疼的问题。我有以下数据
Group
Id Name
1 Group 1
2 Group 2
User
Id Name
1 Jon Williams
2 Mike Williams
3 Joyce Copper
Product
Id Name
1 Cookies
2 Milk
3 Ice cream
Status
Id Name
1 Untouched
2 Consumed
3 Partly Consumed
HouseholdProduct
Id Product User Status
1 1 1 1
2 2 2 1
3 3 3 1
4 1 1 1
5 2 2 1
6 3 3 1
GroupHousehold
GroupId HouseholdProductId
1 1
1 2
1 3
2 1
2 2
2 3
在 Postgres 中,我写了以下内容
select g.id as Group Id, g.name as Group, p.name as Product,
u.name as User
from group g
left join GroupHouse gh on
g.id = gh.groupId
left join HouseHoldProduct hp on
gh.houseHoldProductId = hp.id
left join User u on
hp.userId = u.Id
left join Product p on
hp.product_id = p.id
order by g.id asc
I get the following data which is correct:
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Mike Williams
1 Group 1 Ice cream Joyce Copper
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Mike Williams
2 Group 2 Ice cream Joyce Copper
When I take that exact query and paste it in Hibernate as native query, I get the follow results which is wrong.
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Jon Williams
1 Group 1 Ice cream Jon Williams
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Jon Williams
2 Group 2 Ice cream Jon Williams
看起来休眠是错误地左连接到用户表。有谁知道为什么会这样?有没有办法来解决这个问题?