• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA many-to-many with extra attribute

 
Jair Elton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic