java - Hibernate multiple attribute mappings same table deletion -
i have simple problem in hibernate. have users , projects. users_projects (in case user_project_association) has attribute "role" if user project leader or normal user.
@onetomany(mappedby = "project", cascade = cascadetype.all, orphanremoval = true, fetch = fetchtype.lazy) @lazycollection(lazycollectionoption.extra) private set<userprojectassociation> userprojectassociations = new hashset<>(); @manytomany(fetch = fetchtype.eager) @jointable(name = "user_project_role", joincolumns = @joincolumn(name = "project_id", referencedcolumnname = "id"), inversejoincolumns = @joincolumn(name = "user_id", referencedcolumnname = "id")) @wherejointable(clause = "role_id=7") @setter(accesslevel.none) private set<user> projectleaders = new hashset<>(); @manytomany @jointable(name = "user_project_role", joincolumns = @joincolumn(name = "project_id", referencedcolumnname = "id"), inversejoincolumns = @joincolumn(name = "user_id", referencedcolumnname = "id")) @lazycollection(lazycollectionoption.extra) @setter(accesslevel.none) private set<user> users = new hashset<>();
if try remove project via "getentitymanager().remove(persistentinstance);" can see in log sql statements:
hibernate: /* delete collection com.xxx.core.model.project.users */ delete user_project_role project_id=? hibernate: /* delete collection com.xxx.core.model.project.projectleaders */ delete user_project_role project_id=? , ( role_id=7 ) hibernate: /* delete com.xxx.core.model.userprojectassociation */ delete user_project_role id=?
and error message:
2016-05-25 10:05:57 info abstractbatchimpl:208 - hhh000010: on release of batch still contained jdbc statements
and nothing deleted. have idea projects deleted?
thanks lot :)
puh. found (the or a) solution it. it's not elegant, it's working:
@manytomany(fetch = fetchtype.eager) @jointable(name = "user_project_role", joincolumns = @joincolumn(name = "project_id", referencedcolumnname = "id"), inversejoincolumns = @joincolumn(name = "user_id", referencedcolumnname = "id")) @wherejointable(clause = "role_id=7") @sqlinsert(sql = "insert user_project_role (project_id, user_id, role_id) values (?, ?, 7)") @sqldelete(sql = "delete user_project_role project_id=? , user_id=? , role_id = 7") @setter(accesslevel.none) private set<user> projectleaders = new hashset<>(); @manytomany @jointable(name = "user_project_role", joincolumns = @joincolumn(name = "project_id", referencedcolumnname = "id"), inversejoincolumns = @joincolumn(name = "user_id", referencedcolumnname = "id")) @lazycollection(lazycollectionoption.extra) @wherejointable(clause = "role_id=8") @sqlinsert(sql = "insert user_project_role (project_id, user_id, role_id) values (?, ?, 8)") @setter(accesslevel.none) private set<user> projectusers = new hashset<>();
so removed userprojectassociations attribute , added customized @sqlinsert , @sqldelete statements.
if want users including normal users , project leaders use method:
public set<user> getallusers() { set<user> allusers = new hashset<>(); allusers.addall(projectleaders); allusers.addall(projectusers); return allusers; }
Comments
Post a Comment