java - removing entity with unidirectional relationship @ManyToOne (JPA) -


i have problem, becouse don't know how remove specific role user. problem connected unidirectional relation in permission class. these relevant code snippets.

class person

@entity public class user {  @id @generatedvalue(strategy = generationtype.identity) private long userid;  @onetomany(cascade = cascadetype.all, fetch=fetchtype.lazy, mappedby="user") private collection<role> roles; 

class role

@entity public class role {  @id @generatedvalue(strategy = generationtype.identity) private long roleid;  @manytoone( fetch=fetchtype.lazy) private user user; 

i tried remove specific role user in way

user.getroles().remove(roletoremove) entitymanger.merge(user) 

roletoremove deleted collection not datebase. (i wrote test completed)

so, added

 orphanremoval = true 

and have problem unidirectional relationship occurs in permission class. receives databaseexepction becouse idrole want remove, exists in permision table.

 @entity public class permission {  @id @generatedvalue(strategy = generationtype.identity) private long permissionid;  @manytoone( fetch=fetchtype.lazy) private role role; 

so, question how manage problem.

hello user not owner of relationship user - role. need update owning end of relationship in order make work.

user.getroles().remove(roletoremove) roletoremove.setuser(null) entitymanger.merge(user) 

my understanding don't need orphan removal.


Comments