views:

63

answers:

1

Hi, I'm pretty 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!

A: 

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
Anthony Bishopric
yes, I've been asking around and several people recommended the same... But I don't feel good inside when I do this kind of things... thanks!
Joaquín L. Robles