Hibernate version: 3.0
Yes I do own Hibernate in Action and it does not really answer this question...
I have the following relationship between two objects: a Client and a Project. A client can have as many projects as he wants (one-to-many) and a project can have only one client as a parent (many-to-one).
Here are the .hbm.xml files:
Client:
Code:
...
<class name="Client" table="clients">
<cache usage="read-write"/>
<id name="ID" column="ID" type="java.lang.Long">
<generator class="native"/>
</id>
...
<set name="projects" table="projects" lazy="true" cascade="all">
<cache usage="read-write"/>
<key column="clientID"/>
<one-to-many class="org.ablogic.projects.Project"/>
</set>
...
Here's Project:
Code:
...
<class name="Project" table="projects">
<cache usage="read-write"/>
<id name="ID" column="ID" type="java.lang.Long">
<generator class="native"/>
</id>
<many-to-one name="client" class="org.ablogic.clients.Client" column="clientID" cascade="save-update" />
...
First question: are the "cascade" relationships correct? If my client is deleted, I want all the projects to be deleted as well - therefore it's "all".
If a project is deleted - I don't want to delete the client, but I still want to keep save/update going (even though it doesn't seem to work - I still have to delete objects manually).
So am I on the right track here?
Ok, now here's the CRUD actions in my struts action file:
Code:
public class ProjectCRUDAction extends DispatchAction
{
public ActionForward create( ActionMapping mapping, ActionForm aform, HttpServletRequest request,
HttpServletResponse response )
{
ProjectForm form = (ProjectForm)aform;
Project project = new Project();
form.copyTo(project);
HibernateUtil.beginTransaction();
HibernateUtil.getSession().save(project);
project.getClient().getProjects().add(project);
HibernateUtil.getSession().update(project.getClient());
HibernateUtil.commitTransaction();
request.setAttribute("project",project);
return mapping.findForward("success");
}
public ActionForward edit( ActionMapping mapping, ActionForm aform, HttpServletRequest request,
HttpServletResponse response )
{
ProjectForm form = (ProjectForm)aform;
Project project = form.getProject();
if (! project.getClient().equals(form.getClient()) )
project.getClient().getProjects().remove(project);
form.copyTo(project);
project.getClient().getProjects().add(project);
HibernateUtil.beginTransaction();
HibernateUtil.getSession().update(project);
HibernateUtil.getSession().update(project.getClient());
HibernateUtil.commitTransaction();
request.setAttribute("project",project);
return mapping.findForward("success");
}
public ActionForward delete( ActionMapping mapping, ActionForm aform, HttpServletRequest request,
HttpServletResponse response )
{
ProjectForm form = (ProjectForm)aform;
Project project = form.getProject();
project.getClient().getProjects().remove(project);
HibernateUtil.beginTransaction();
HibernateUtil.getSession().delete(project);
HibernateUtil.getSession().update(project.getClient());
HibernateUtil.commitTransaction();
request.setAttribute("project",project);
return mapping.findForward("success");
}
}
So that's pretty muc creating/editing/deleting (according to the method name).
It seems that I'm doing an awful lot of extra work here - such as removing the project from the old client, and adding it to new client, and setting all those bi-directional references...
Well my question is - am I doing all the neccessary work, or is some of it redundant?
Thanks for your replies!