开发者

Hibernate Composite primary key contains foreign key which is subset of other tables primary key, how to map?

The scenario is as follows

Table A has a composite PK.

Table A 

--------

a1 (pk)开发者_JS百科

a2 (pk)

a3 (pk)

foo

bar

LastUpdateDate

LastUpdateUser ========

Table B has a composite PK too which is actually subset of A's PK, here is working as FK as well. There is no forced FK constraint.

Table B 

-------- a1 (fk,pk) a2 (fk,pk) foo bar LastUpdateDate LastUpdateUser ========

I tried several ways, and none of them works. Can anyone tell a working Hibernate mapping solution? better in annotation style.

Thanks a lot.

Viv


I can see following set up. Although it may be tricky to get to work and, based on my experience, it's unnecessary complexity.

class A implements Serializable {
   @Id
   private PK id = new PK();
   @Embeddable      
    public static final class PK implements Serializable {
        @ManyToOne 
        @JoinColumns ({
        @JoinColumn(name="A1_COL", referencedColumnName = "id.a1"),
        @JoinColumn(name="A2_COL", referencedColumnName = "id.a2"),

        })
         B b;            
        @Basic String a3;
    }    
 }


class B implements Serializable {
   @Id
   private PK id = new PK();
   @Embeddable      
    public static final class PK implements Serializable {                    
        @Basic String a1;
        @Basic String a2;
        @Basic String a3;
    }    
 }

You should be able to access be via

a.getId().getB()

Add setters/getters as necessary.

Usually this type of problems requires a lot of trial and error, so don't expect this to work right away. But that should give you a direction for digging. Once you nail down final configuration, please add it to you question so somebody can benefit from it in future.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜