-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: same primary key as foreign key in two table
PostPosted: Mon Jan 21, 2008 1:30 pm 
Newbie

Joined: Tue Nov 13, 2007 10:02 am
Posts: 1
Hi All,

schema

TABLE_A
=========
id_a[PK]
tx_a_desc

TABLE_B
=========
id_b[PK]
id_a[FK]
tx_b_desc

TABLE_C
=========
id_c
id_a[FK]
tx_c_desc

With id_c and id_a as composite primary key for TABLE_C

What should be the best way to map these?
It’s not normal but I can't change the schema.

I am trying something like this for now

A.java [@Table(name = "TABLE_A")]
Code:
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "id_a", nullable = false)
  public Set<B> getB() {
    return b;
  }
 
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "id_a", nullable = true)
  public Set<C> getC() {
    return c;
  }   


B.java [@Table(name = "TABLE_B")]
Code:
   
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_a", nullable = false, insertable = false, updatable = false)
    public A getA() {
      return this.a;
  }


C.java [@Table(name = "TABLE_C")]
Code:
   
@AttributeOverrides( { @AttributeOverride(name = "id_a", column = @Column(name = "id_a", nullable = false)),
        @AttributeOverride(name = "id_c", column = @Column(name = "id_c", nullable = false))})
        public CompositeId getId() {
       return id;
  }


CompositeId.java [@Embeddable]
Code:
   
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_a", nullable = false, insertable = false, updatable = false)
    public A getA() {
      return this.a;
  }


It works but it's wrong and may break in certain cases.
Ideally nullable should be false in both the cases in A's mapping,
But when I try this I get Repeated column in mapping


Please help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.