Java Hibernate Batch Delete

public void RemoveAllRoleOfUser(int pUserId) throws AppException
{
try
{
if(session==null)
{
session=GetSession();
}

/*
List<RoleAssign> lstRoleAssigns=GetAllRoleAssignOfUser(pUserId);
if(!lstRoleAssigns.isEmpty()&&lstRoleAssigns.size()>=1)
{
for(RoleAssign ra:lstRoleAssigns)
{
session.delete(ra);
session.flush();
}
}
*/

/**
//这种实现方法可以考虑,但是总感觉只改了数据库,实体层的对象会不会没有同步
//test result: pass
int result=session.createSQLQuery("delete from ROLE_ASSIGN where FK_USER_ID="+pUserId).executeUpdate();
session.flush();

//test result: pass
//int result=session.createQuery("delete RoleAssign ra where ra.targetUser.userId="+pUserId).executeUpdate();
//session.flush();
*/

//test result: fail
//session.delete("from RoleAssign ra where ra.targetUser.userId="+pUserId);

}
catch(Exception ex)
{
throw new AppDAOException(ex.getMessage());
}
}

你可能感兴趣的:(java,Hibernate)