|
Hi all am a bit of a newbie to Hibernate and was just trying to get over an issue with 1to1 and 1toMany mappings.
Just to describe system. Have simple user/role system.
Consist of object model User, Role and Privilege. A User has one Role object and a Role object has an array of Privilege objects.
Users 1to1 Role relationship is described by role_fk on User table.
And Role to Privilege 1toMany relationship is described by a link table with a role_id and privilege_id.
It all works well, including deleting a role, which will delete the deleted roles entries in the link table.
The problem is I dont understand how to clean up when you delete a child. THat is if i delete a privilege that is involved in a 1toMany relationship with a role it does not delete the entries in the link table. But yet the relationship has been removed. I thought this was because it was cached, but i restarted the app server and even though the entry in the link table remained, the loading of the roles Privilege array did not fail. ( actually this is probalby due to not setting the fail if no t present attribute,cant remember it).
Also the same happens for my 1to1 relationship betwee user and role. If ya delete the role, the fk column on the user table keeps the key, doesnt return to null. This does cause failing when ya then try to get the user.
I am sorry about this question, because i know hibernate must allow this functionality its just that with the different cascade options i have beciome a bit lost on what works and what doesnt.
Note these arent bidirectional, that is once an object is deleted, doesnt mean child or parents should be deleted.
I will only include the mapping files and the pojos, because configuration is all ok (dao called from stateless sessionbean, running in jboss, using CMT, but have tested from standalone configuration where i do the commiting and transaciton management and same issues)
Here is object model. (left out getters/setters)
User implements Serializable {
private java.lang.String id;
private java.util.Date dataofbirth;
private Role userrole;
private java.lang.String name;
private java.lang.String password;
}
Role implements Serializable {
private java.lang.String id;
private Privilege[] privies;
private java.lang.String roleName;
private java.lang.Integer superUser;
}
Privilege implements Serializable {
private java.lang.String id;
private java.lang.String privName;
private java.lang.Integer privValue;
}
and the mapping files.
User.hb.xml
<hibernate-mapping package="org.user.user.data" default-cascade="delete">
<class name="User" table="usertb" lazy="false" >
<id name="id" type="text" column="id" >
</id>
<property name="dataofbirth" type="java.util.Date" column="dataofbirth" />
<property name="name" type="text" column="name" />
<property name="password" type="text" column="password" />
<many-to-one
name="userrole"
class="org.user.role.data.Role"
column="userrole_fk"
cascade="all,delete"
foreign-key="true"
/>
</class>
</hibernate-mapping>
Role.hbm.xml
<hibernate-mapping package="org.user.role.data">
<class name="Role" table="roletb" lazy="false" >
<id name="id" type="text" column="id" >
</id>
<property name="roleName" type="text" column="rolename"/>
<property name="superUser" type="integer" column="superuser" />
<array name="privies"
table="role_privies_link"
cascade="persist">
<key column="role_id"/>
<list-index column="sorterx"/>
<many-to-many column="privies_id" class="org.user.privilege.data.Privilege" />
</array>
</class>
</hibernate-mapping>
and Privilege.hbm.xml
<hibernate-mapping package="org.user.privilege.data" >
<class name="Privilege" table="privilegetb" lazy="false" >
<!-- <comment>Privilege of the system.</comment>-->
<id name="id" type="text" column="id" >
</id>
<property name="privName" type="text" column="privname"/>
<property name="privValue" type="integer" column="privvalue" />
</class>
</hibernate-mapping>
I will include one DAO, but they are all the same besides the objects.
public class RoleHibernateDAO implements RoleDAO {
private static final Logger logger = Logger.getLogger(RoleHibernateDAO.class.getName());
public RoleHibernateDAO() {
}
public Role createRole(Role role) throws RoleDAOException {
try {
logger.info("create new role");
Session session = HibernateHelper.getSession();
session.save(role);
return role;
} catch (HibernateException e) {
logger.severe("Unable to create role " + e.getMessage());
throw new RoleDAOException("Unable to create role " + e.getMessage());
}
finally {
HibernateHelper.closeSession();
}
}
public void deleteRole(String id) throws RoleDAOException {
try {
logger.info("create new role");
Session session = HibernateHelper.getSession();
Role role = (Role)session.load(Role.class, id);
session.update(role);
session.delete(role);
} catch (HibernateException e) {
logger.severe("Unable to delete role " + e.getMessage());
throw new RoleDAOException("Unable to delete role " + e.getMessage());
}
finally {
HibernateHelper.closeSession();
}
}
public void updateRole(Role role) throws RoleDAOException {
try {
logger.info("update role");
Session session = HibernateHelper.getSession();
session.update(role);
} catch (HibernateException e) {
logger.severe("Unable to update role " + e.getMessage());
throw new RoleDAOException("Unable to update role " + e.getMessage());
}
finally {
HibernateHelper.closeSession();
}
}
public Role getRole(String id) throws RoleDAOException {
try {
logger.info("get role");
Session session = HibernateHelper.getSession();
Role role = (Role)session.load(Role.class, id);
return role;
} catch (HibernateException e) {
logger.severe("Unable to get role " + e.getMessage());
throw new RoleDAOException("Unable to get role " + e.getMessage());
}
finally {
HibernateHelper.closeSession();
}
}
public Role[] getAllRoles() throws RoleDAOException {
try {
logger.info("get role");
Session session = HibernateHelper.getSession();
List list = session.createQuery("from Role").list();
Role[] allRoles = new Role[list.size()];
list.toArray(allRoles);
return allRoles;
} catch (HibernateException e) {
logger.severe("Unable to getAll roles " + e.getMessage());
throw new RoleDAOException("Unable to getall roles " + e.getMessage());
}
finally {
HibernateHelper.closeSession();
}
}
}
|