0

まず、私はかなり熟練した 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

休止状態が User テーブルに正しく結合されていないようです。なぜこれが起こっているのか誰にも分かりますか?これを修正する方法はありますか?

4

0 に答える 0