Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JPA: problem with many to many relations

Reply
Thread Tools

JPA: problem with many to many relations

 
 
loris_p
Guest
Posts: n/a
 
      07-06-2008
Hi! I'm writing a desktop application using JPA. I have two entities
(User and Movement) with a many to many relation.
This is the part of Movment.java:

@ManyToMany
@JoinTable(
name="USERS_MOVEMENTS",
joinColumns=
@JoinColumn(name="MOVEMENT_ID",
referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="USER_ID", referencedColumnName="ID")
)
private List<User> users;

and User.java:

@ManyToMany(mappedBy="users")
private List<Movement> movements;

Obviously I have also getters and setters.
users_movements join table has 2 fields: user_id and movment_id.
All seems to work fine, i.e. if I have a record user_id=1 and
movement_id=1 and I assign the movement 1 to user 2 JPA creates a
delete query for 1,1 record and an insert for user_id=2 and
movement_id=1. The problem is when I have a movement associated with 2
users, i.e. movement 1 with user 1 and 2 and I want to delete one of
the two relations: JPA creates a delete query for the relation to
remove, and an insert query for the relation that should remain. The
result is an exception caused by the insert of a duplicate key.
Did I define the relation in the wrong way or did I miss something?
Thanks in advance.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem display several DataTables with relations Mikaël PLOUHINEC ASP .Net Datagrid Control 0 12-21-2006 10:07 AM
problem on saving object relations. shuisheng C++ 3 10-31-2006 06:19 AM
data relations Joe Van Meer ASP .Net 4 05-05-2004 06:57 PM
data relations and datasets inquiry Joe Van Meer ASP .Net 0 05-05-2004 01:30 PM
Can't decide between Relations::Family and Class::DBI Nicolas STAMPF Perl 0 05-04-2004 12:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57