-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with Composite-id
PostPosted: Tue Aug 07, 2007 5:21 am 
Newbie

Joined: Thu Jun 14, 2007 8:10 am
Posts: 7
Hi,
I have a problem with two tables with composite-ids.

First Table:
    FirstTablePK(PK)
    Name

Second Table:
    FirstTablePK(FK)(PK)
    SecondTablePK(PK)
    Name


I know it's a bad design, but it's used in production and I can't change it.

Now I tried to map that with annotations to classes. The first class is working fine, but I don't know how to map the second class. I used the Embedded-Annotaion, but it doesn't work properly.

Here are my classes
Code:
@Entity
@Table(name="FirstTable")
public class FirstClass implements Serializable{
   @Id
   @Column(name="FirstTablePK")
   @Type(type="integer")
   private Integer id;

   @Column(name="Name")
   @Type(type="string")
   private String name;
}


Code:
@Entity
@Table(name="SecondTable)
public class SecondClass implements Serializable{
   @Id
   private SecondTablePK id;

   @Column(name="Name")
   @Type(type="string")
   private String name;

   @Embeddable
   public class FilialePK implements Serializable{
      private Integer id;
      private FirstClass firstClassImpl.
   }
}


Now I don't know how to make this working. I searched for some examples in the web but didn't found much...
Does anybody know how to solve this....?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 6:37 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you're getting an error something like "could not determine type..." try specifying the relationship to FirstClass in SecondClassPK:
Code:
public class FilialePK implements Serializable{
      private Integer id;
      @ManyToOne   // <-------- HERE
      private FirstClass firstClassImpl.
   }

If you're having other problems you need to post the details.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 6:47 am 
Newbie

Joined: Thu Jun 14, 2007 8:10 am
Posts: 7
Hi thatmikewilliams,

thanks for your post.

Don't I have to tell hibernate which columns in the table to take. Where from does hibernate know that the SecondTablePK in the SecondTable is the id-Attribute of the PK-Class.

What kind of details do you mean?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 6:48 am 
Newbie

Joined: Thu Jun 14, 2007 8:10 am
Posts: 7
I made a mistake in the first post, of course it has to be SecondTablePK and not FilialePK at the class declaration...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 7:20 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can specify the column name with @JoinColumn

e.g.
Code:
public class FilialePK implements Serializable{
      private Integer id;
      @ManyToOne
      @JoinColumn(name = "FirstTablePK")
      private FirstClass firstClassImpl.
   }
Quote:
Where from does hibernate know that the SecondTablePK in the SecondTable is the id-Attribute of the PK-Class

I'm not sure I understand what you mean. You've defined a composite PK in the second table. The id attribute of the SecondTablePk class is just one part of the second table's PK.

Quote:
What kind of details do you mean?

Error messages, stack traces, sql, etc. See:
http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.