开发者

Composite ids between multiple tables in Hibernate

开发者 https://www.devze.com 2023-01-15 19:18 出处:网络
I\'m pretty开发者_如何学C new with Hibernate, and I\'m facing some trouble working with composite ids and foreing keys,

I'm pretty开发者_如何学C new with Hibernate, and I'm facing some trouble working with composite ids and foreing keys, I just want to make this work:

=======    =======
 Table A    Table B
=======    =======
atr1 PK    atr1 FK(Table A, atr1) PK
atr2 PK    atr2 FK(Table A, atr2) PK
atr3       atr3 FK(Table C, atrN) PK
           atr4 

And the relation is the following: A has (knows) none ore more instances of B (a list of them), and B has (knows) just one instance of C (this is not a problem by now).

I've read something about using classes to point composite ids (in the official tutorial), overriding equals() and hashCode() but I can't make this work because I'm pretty confused about how to write my hbm.xml files...

Any help? Thanks!


You might find yourself happier if you just simplify the data model and get rid of some redundancy. Dealing with Hibernate on more complex data modeling questions like this can result in frustration later even if you do get it working. Maybe consider giving B a simple A_id instead of the properties on A itself (seeing as how they map exactly onto the primary key of A)

    ___A___
    id PK
    atr1
    atr2
    atr3
    --> composite unique(atr1, atr2)

    ___B___
    id PK
    a_id FK (A.id)
    c_id FK (C.id)
    atr4
    --> composite unique(a_id, c_id)

    ___C___
    id PK
    atrN  unique
0

精彩评论

暂无评论...
验证码 换一张
取 消