-->
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.  [ 2 posts ] 
Author Message
 Post subject: Composite Id but only one foreign key
PostPosted: Tue Jul 20, 2010 5:25 am 
Newbie

Joined: Thu Jul 31, 2008 5:35 am
Posts: 2
Hi everyone,

I've got a problem with composite Ids and hiberante docs don't help me further.
I have two tables that both have composite Ids and Many-to-One relationship between each other. Furthermore the foreign key consists of only one identifier, the database model is following:
-------------------------------------------------
Table InAttribute:
att_id pk
ver_id pk
obj_id fk //many-to-one
floatvalue
....
-------------------------------------------------
Table InObject
obj_id pk
ver_id pk
...
-------------------------------------------------
As you see, ATTRIBUTES are linkes unidirectionally Many-to-One to OBJECTS through obj_id foreign key (but not through both obj_id and ver_id). Everything that I tries before, didn't work. So I implemented composite id for InObject and InAttribute that way:

Code:
   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "objId", column = @Column(name = "OBJ_ID", nullable = false)),
         @AttributeOverride(name = "verId", column = @Column(name = "VER_ID", nullable = false)) })
   public InObjectId getId() {
      return this.id;

   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "attId", column = @Column(name = "ATT_ID", nullable = false)),
         @AttributeOverride(name = "verId", column = @Column(name = "VER_ID", nullable = false)) })
   public InAttributeId getId() {
      return this.id;
   }
   }


InAttribute has following reference to InObject (ManyToOne):

Code:
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
        @JoinColumn(name="OBJ_ID")
   public InObject getInObject(){
      return inObject;
   }


but I've got an error from hebrnate, because the number of foreign keys should be 2.

The reference in InObject looks like this:

Code:
    @OneToMany(mappedBy="inObject", fetch=FetchType.LAZY)
    public Collection<InAttribute> getInAttributes(){
       return inAttributes;
    }


I can't find information about such situations because hibernate documentation handles tables where number of foreign keys is equal to the number of primary keys.

Thanks in advance!
Yuriy


Top
 Profile  
 
 Post subject: Re: Composite Id but only one foreign key
PostPosted: Tue Jul 20, 2010 7:07 am 
Newbie

Joined: Tue Jul 20, 2010 1:53 am
Posts: 18
can make it as table with different Columns each other columns. But one thing don't created Foreign key(other table) first table same Columns ...Than you can make .

Eg Table1: table2

Id Name Desg Sal No No DOB . . . .
1 x I 100 A1 A1 jun 2

2 y j 200 A2 A2 Aug12

3 z k 300 ..

4 a l 400 ...

4 b M 500 ..



See above tables you can create composite Id(Id ,Name) than only solve your problem


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

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.