Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ManyToMany mapping leads to duplicate entry error in join table, why is it?(Please help me! ManyToMany mapping leads to duplicate entry error in join table, why is it?)
icon8.gif  ManyToMany mapping leads to duplicate entry error in join table, why is it? [message #691883] Sat, 02 July 2011 17:50 Go to next message
Pal Konyves is currently offline Pal KonyvesFriend
Messages: 4
Registered: July 2011
Junior Member
Hi, I'm Pal, and I keep getting a strange error. Smile

I want to create custom JAAS authentication where my users and principals relationships are defined in JPA as shown:

class AuthUser

public class AuthUser implements Serializable {

    // own properties
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private int uid;

    @Column(name="NAME",unique=true)
    private String name;

    // Join classes
    @ManyToMany(fetch=FetchType.EAGER,cascade={CascadeType.MERGE})
    @JoinColumn(name="PRINCIPALS_PRINCIPAL")
    private Set<AuthPrincipal> principals;
}


class AuthPrincipal

public class AuthPrincipal implements Serializable {

    // Defining roles
    public enum Principal {
        AUTHUSER, STUDENT, ADMINISTRATOR, TEACHER
    }

    @Id
    @Enumerated(EnumType.STRING)
    @Column(name="PRINCIPAL")
    private Principal principal;

    @ManyToMany(mappedBy = "principals")
    @JoinColumn(name="USERS_USER")
    private Set<AuthUser> users;
}


Maps to the following Table definition


Table authprincipal
===================
PRINCIPAL        varchar(255) PK

Table authuser
==============
UID              int(11)      PK
EMAIL            varchar(255)
NAME             varchar(255)
PASSWORD         varchar(255)

Table authuser_authprincipal
============================
users_UID            int(11)      PK
principals_PRINCIPAL varchar(255) PK


Now, I created a JSF file from which I call an action method that calls this one:

public void createUser(AuthUser newUser) throws UserNameExistsException, UserEmailExistsException {
    AuthPrincipal role = authRoleFacade.find(AuthPrincipal.Principal.AUTHUSER);
    if( role == null ){
        role = new AuthPrincipal();
        role.setPrincipal(AuthPrincipal.Principal.AUTHUSER);
        authRoleFacade.create(role);
    }    
    authUserFacade.create(newUser);
    addPrincipalToUser(newUser, role);
}


The actual problem
I can create the first user. But I can't create the second user . Notice that at the second user I use the existing role object, and only cascade a merge operation, I don't know if it mattress.

The strangest thing is that it says it duplicates the 2-AUTHUSER key where 2 is the id of the new user so cannot already be in the join table. What is wrong with it or with Eclipselink or with Me?

The error what eclipselink throws

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2-AUTHUSER' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO AUTHUSER_AUTHPRINCIPAL (principals_PRINCIPAL, users_UID) VALUES (?, ?)
        bind => [2 parameters bound]
Query: DataModifyQuery(name="principals" sql="INSERT INTO AUTHUSER_AUTHPRINCIPAL (principals_PRINCIPAL, users_UID) VALUES (?, ?)")
jpa many-to-many duplicates eclipselink duplicate-entry
Re: ManyToMany mapping leads to duplicate entry error in join table, why is it? [message #691955 is a reply to message #691883] Sat, 02 July 2011 23:11 Go to previous messageGo to next message
Rolf Paulsen is currently offline Rolf PaulsenFriend
Messages: 14
Registered: June 2010
Junior Member
Hi,
what is the state of "Set<AuthUser> users" of your existing principal object in the moment of merge?
Empty or containing the first user?
What do you merge: principal or user?

Re: ManyToMany mapping leads to duplicate entry error in join table, why is it? [message #692056 is a reply to message #691955] Sun, 03 July 2011 11:00 Go to previous message
Pal Konyves is currently offline Pal KonyvesFriend
Messages: 4
Registered: July 2011
Junior Member
You are absolutelly right!

1) The problem occured because of my fault, though, I don't know why the error log talked about user id 2 when I only had one user, but i'm gonna tell a possible reason

The problem was: I wanted to persist an already persisted user. In my @SessionScoped @ManagedBean I created an AuthUser in the constructor. In the first registration it got persisted but I did not create a fresh one. When I wanted to register the next one what my program actually did was: changed the username, email and password of the already persisted AuthUser and wanted to persist it again.

Back to 1) I can imagine when I called persist the second time, Eclipselink actually persisted my Entity updating the user id to 2 in both the AuthUser table and the join table. Afterwards because I defined the Merge operation on AuthUser.principals, it wanted to update again the join table and that's when it messed up. If I had looked closely to the generated Queries in the log file, I think I could have figured it out myself.

Thank for the hint!
Previous Topic:Programmatically obtain SQL statements emitted by EclipseLink
Next Topic:Problem having 2 different models
Goto Forum:
  


Current Time: Fri Mar 29 12:36:50 GMT 2024

Powered by FUDForum. Page generated in 0.02307 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top