-->
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: Mapping Table with 3 ManyToOne assosiated pk fields
PostPosted: Mon Dec 20, 2010 8:13 am 
Newbie

Joined: Mon Dec 20, 2010 7:58 am
Posts: 1
Hi,

I am new to Hibernate & JPA and I decided to use Hibernate instead of Ibatis at may new project. But now a little worried about deadline.
I am reading hibernate jpa docs and it was awesome for simple mappings. But now get stuck with mapping TableC with 3 ManyToOne assosiated pk fields.

TableA
+ aId (pk)

TableB
+ bId (pk)

TableC
+ aId (pk)
+ old_bId (pk)
+ new_bId (pk)

Thanks for your advice.


Top
 Profile  
 
 Post subject: Re: Mapping Table with 3 ManyToOne assosiated pk fields
PostPosted: Wed Dec 22, 2010 1:25 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
The general pattern for something like that is:

- Create an @Embeddable class to hold all the raw types (3 ints, or 3 longs, or whatever your PK type is) named after the columns to keep things simple).
- Map it in your TableC entity class with @EmbeddedId.
- Map the two foreign key properties as you normally would, with @ManyToOne and @JoinColumn. Add "insertable=false" and "updatable=false" to the @JoinColumn mappings.
- Now you can refer to these non-insertable and non-updatable properties in the mappedBy attribute of your @OneToMany properties in TableA and TableB's entities.

The JPA2 way of doing it would be to use @MapsId annotations, but I've had problems with that and Hibernate so I just use the old method described above.


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.