JPA: problem with many to many relations

Discussion in 'Java' started by loris_p, Jul 6, 2008.

  1. loris_p

    loris_p Guest

    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.
     
    loris_p, Jul 6, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicolas STAMPF
    Replies:
    0
    Views:
    421
    Nicolas STAMPF
    May 4, 2004
  2. Joe Van Meer

    data relations and datasets inquiry

    Joe Van Meer, May 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    439
    Joe Van Meer
    May 5, 2004
  3. shuisheng
    Replies:
    3
    Views:
    347
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Oct 31, 2006
  4. markspace

    JSF/JPA problem

    markspace, Sep 13, 2011, in forum: Java
    Replies:
    12
    Views:
    3,903
    Arved Sandstrom
    Sep 14, 2011
  5. Mikaël PLOUHINEC

    Problem display several DataTables with relations

    Mikaël PLOUHINEC, Dec 21, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    692
    Mikaël PLOUHINEC
    Dec 21, 2006
Loading...

Share This Page