java - Hibernate deletes Lazy loaded many-to-many collection when a get call is made on the collection -


i trying fetch roles database using hibernate in java application. undergoing many many mapping same.

after fetching data, getting deleted database. not calling delete method either still deletion occurs.

role.java

@entity @table(name = "role")  public class role extends basemodel implements java.io.serializable {  private long id; private set<user> users = new hashset<user>(0);    @id @column(name = "id", unique = true, nullable = false, scale = 0) public long getid() {     return this.id; }  public void setid(long id) {     this.id = id; }   @manytomany(fetch = fetchtype.lazy, mappedby = "roles") @jointable(name = "userrolemap", joincolumns = { @joincolumn(name = "roleid", nullable = false, updatable = false) }, inversejoincolumns = { @joincolumn(name = "userid", nullable = false, updatable = false) }) public set<user> getusers() {     return this.users; }  public void setusers(set<user> users) {     this.users = users; }  } 

user.java

@cacheable @entity @table(name = "user", uniqueconstraints = @uniqueconstraint(columnnames = "loginid")) public class user extends basemodel implements java.io.serializable {  private long id;  private set<role> roles = new hashset<role>(0);   @id @column(name = "id", unique = true, nullable = false, scale = 0) public long getid() {     return this.id; }  public void setid(long id) {     this.id = id; }   @manytomany(fetch = fetchtype.lazy, cascade = cascadetype.all) @jointable(name = "userrolemap", joincolumns = { @joincolumn(name = "userid", nullable = false, updatable = false) }, inversejoincolumns = { @joincolumn(name = "roleid", nullable = false, updatable = false) }) public set<role> getroles() {     return this.roles; }  } 

we trying roles database using following code snippet

public list<string> rolesasga() {     list<string> proxyuserroles = new arraylist<string>();     iterator<role> itr = getroles().iterator();     while (itr.hasnext()) {         proxyuserroles.add(new simplegrantedauthority(itr.next()                 .getrolename()));     }     return proxyuserroles; } 

after fetching roles data (corresponding role) getting deleted simultaneously, can tell me why?

edit - debugging in eclipse , hibernate marking collection removal since, currentpersistor becomes null collection entity. debug further , post update it.

edit 1 missed mention that, user @cacheable, , being fetched ehcache, when getrole call being made, collection gets loaded prompty queued deletion. removing @cacheable annotation fixes problem. create seperate question regarding @cacheable , manytomany or shall update question , hope proper solution?

i guess causing issue

private set<role> roles = new hashset<role>(0);  @manytomany(fetch = fetchtype.lazy, cascade = cascadetype.all) @jointable(name = "userrolemap", joincolumns = { @joincolumn(name = "userid", nullable = false, updatable = false) }, inversejoincolumns = { @joincolumn(name = "roleid", nullable = false, updatable = false) }) public set<role> getroles() {     return this.roles; } 

it should be

 private set<role> roles;  @manytomany(fetch = fetchtype.lazy, cascade = cascadetype.all) @jointable(name = "userrolemap", joincolumns = { @joincolumn(name = "userid", nullable = false, updatable = false) }, inversejoincolumns = { @joincolumn(name = "roleid", nullable = false, updatable = false) }) public set<role> getroles() {     if(roles == null){          return new hashset<role>;     }     return this.roles; } 

because whenever getroles called, return empty set. hibernate assuming have emptied/deleted set , deleting them in database


Comments