aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA many-to-many with extra attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA many-to-many with extra attribute" Watch "JPA many-to-many with extra attribute" New topic
Author

JPA many-to-many with extra attribute

Jair Elton
Greenhorn

Joined: Jan 19, 2007
Posts: 9
Hi,

I have this tables:

Table: users

id integer
name varchar

Table: user_contacts

user_id integer
contact_id integer
active boolean

This is a self many-to-many relationship (user_id and contact_id is an user) with an extra attribute.

How I can map this with JPA?

With hibernate I do this:



Thank you!

Jair


Jair Elton<br /><a href="http://jairelton.com" target="_blank" rel="nofollow">http://jairelton.com</a>
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
In Hibernate you were in-lining a composite object and defining a 1-m relationship to te contact object, with a m-1 back from the contact to the user. Similarly, in JPA you would define a contact entity and a 1-m relationship from user to contact and m-1 from contact to user. The only difference is that in JPA contact is a 1st class entity instead of a second class composite.


-Mike
Pro JPA 2: Mastering the Java Persistence API
 
Don't get me started about those stupid light bulbs.
 
subject: JPA many-to-many with extra attribute
 
Similar Threads
Delete/update associations question
Hibernate Composite keymapping
Can't map child List w/ composite ID
HBM mapping Problem
Hibernate Newbie - 1 to Many Relation Tutorial