wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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

JPA many-to-many with extra attribute

Jair Elton

Joined: Jan 19, 2007
Posts: 9

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 Elton<br /><a href="http://jairelton.com" target="_blank" rel="nofollow">http://jairelton.com</a>
Mike Keith
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.

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
Hibernate Composite keymapping
HBM mapping Problem
Delete/update associations question
Can't map child List w/ composite ID
Hibernate Newbie - 1 to Many Relation Tutorial