many to many - JPA, duplicate entry on persist in join table when using @ManyToMany relationship -


i want create custom jaas authentication users , principals relationships defined in jpa 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 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, created jsf file call action method calls 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 can create first user. can't create second user . notice @ second user use existing role object, , cascade merge operation.

the strangest thing says duplicates 2-authuser key 2 id of new user cannot in database. wrong or eclipselink or me?

the error eclipselink throws

internal exception: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: duplicate entry '2-authuser' key 'primary' error code: 1062 call: insert authuser_authprincipal (principals_principal, users_uid) values (?, ?)         bind => [2 parameters bound] query: datamodifyquery(name="principals" sql="insert authuser_authprincipal (principals_principal, users_uid) values (?, ?)") 

this fault. not careful enough :)

1) problem occured because of fault, though, don't know why error log talked user id 2 when had 1 user, i'm gonna tell possible reason

the problem was: wanted persist persisted user. in @sessionscoped @managedbean created authuser in constructor. in first registration got persisted did not create fresh one. when wanted register next 1 program did was: changed username, email , password of persisted authuser , wanted persist again.

back 1) can imagine when called persist second time, eclipselink persisted entity updating user id 2 in both authuser table , join table. afterwards because defined merge operation on authuser.principals, wanted update again join table , that's when messed up. if had looked closely generated queries in log file, think have figured out myself.

i got hint here: http://www.eclipse.org/forums/index.php/m/692056/#msg_692056


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -