Hibernate version: 3.0
Documentation:
How to update the linkages between two tables?
PS Much of this code has been written while reading the 'Hibernate in Action' document, please believe me, I am trying stuff on my own.
I have two tables: Sections and Menus. Menus contains a list of menu
items and Sections are the divisional components of the menu. The hbm files are as follows:
Code:
<hibernate-mapping>
<class name="com.pancakes.website.controller.form.SectionForm" table="Sections">
<id name="sid">
<generator class="native"/>
</id>
<property name="section"/>
<property name="seq" type="int"/>
<set name="menuitems" inverse="true" cascade="all-delete-orphan">
<key column="sid" on-delete="noaction"/>
<one-to-many class="com.pancakes.website.controller.form.MenuForm" not-found="ignore"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="com.pancakes.website.controller.form.MenuForm" table="Menu">
<id name="mid">
<generator class="native"/>
</id>
<property name="title"/>
<property name="description"/>
<property name="price" type="float" not-null="true"/>
<property name="rating" type="int" not-null="true"/>
<property name="dateFrom" type="date"/>
<property name="dateTo" type="date"/>
<property name="imagename"/>
<set name="reviews" table="Review" fetch="join">
<key column="mid" on-delete="noaction"/>
<one-to-many class="com.pancakes.website.controller.form.ReviewForm" not-found="ignore"/>
</set>
<set name="menuConditions" table="MenuCondition" fetch="join">
<key column="mid" on-delete="noaction"/>
<many-to-many class="com.pancakes.website.controller.form.ConditionForm" column="cid" not-found="ignore" unique="true" lazy="false"/>
</set>
<many-to-one name="sectionBean" column="sid" class="com.pancakes.website.controller.form.SectionForm" not-null="true"/>
<many-to-one name="categoryBean" column="catid" class="com.pancakes.website.controller.form.CategoryForm" not-found="ignore"/>
</class>
</hibernate-mapping>
For this example well look at the menu edit form which contains an html select box with the
different menu sections, assuming the user wants to move a menu item from one section to
another. To setup this form I retrieve the menu item being edited and a list of sections.
I'm using Struts so there is some Struts stuff here but the Hibernate should be clear.
Code:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String szmid = request.getParameter("mid");
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
MenuForm result = (MenuForm) session
.createQuery("from com.pancakes.website.controller.form.MenuForm where mid = ?")
.setString(0, szmid)
.uniqueResult();
List sect = session.createQuery("from com.pancakes.website.controller.form.SectionForm order by seq").list();
result.setSections(sect);
List cat = session.createQuery("from com.pancakes.website.controller.form.CategoryForm order by category").list();
result.setCategories(cat);
request.setAttribute(MenuForm.formName, result);
return mapping.findForward("Next");
}
Next the form appears on the screen and the user changes the select box to modify the current
section for this item to a new section and clicks the save button. Next the is the form that saves
these changes to the database.
Code:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MenuForm mf = (MenuForm) form;
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
if (!isCancelled(request)) {
SectionForm sf = (SectionForm) session.get(SectionForm.class, new Integer(mf.getSid()));
sf.addMenuitem(mf);
session.saveOrUpdate(sf);
}
// Next two lines forces Hibernate to retrieve Reviews (or at least an empty set)
List result = (List) new ArrayList();
result.add(mf);
request.setAttribute(MenuForm.listName, result);
return mapping.findForward("Next");
}
You will also want to know what the "addMenuitem()" function does. So:
Code:
public void addMenuitem(MenuForm mf) {
mf.setSectionBean(this);
menuitems.add(mf);
}
Unfortunately no updates take place. The SQL generated is below:
Code:
Hibernate: select menuform0_.mid as mid, menuform0_.title as title1_, menuform0_.description as descript3_1_, menuform0_.price as price1_, menuform0_.rating as rating1_, menuform0_.dateFrom as dateFrom1_, menuform0_.dateTo as dateTo1_, menuform0_.imagename as imagename1_, menuform0_.sid as sid1_, menuform0_.catid as catid1_ from Menu menuform0_ where menuform0_.mid=?
Hibernate: select sectionfor0_.sid as sid, sectionfor0_.section as section5_, sectionfor0_.seq as seq5_ from Sections sectionfor0_
Hibernate: select categoryfo0_.catid as catid, categoryfo0_.category as category6_ from Category categoryfo0_
Hibernate: select sectionfor0_.sid as sid0_, sectionfor0_.section as section5_0_, sectionfor0_.seq as seq5_0_ from Sections sectionfor0_ where sectionfor0_.sid=?
Hibernate: select menuitems0_.sid as sid1_, menuitems0_.mid as mid1_, menuitems0_.mid as mid0_, menuitems0_.title as title1_0_, menuitems0_.description as descript3_1_0_, menuitems0_.price as price1_0_, menuitems0_.rating as rating1_0_, menuitems0_.dateFrom as dateFrom1_0_, menuitems0_.dateTo as dateTo1_0_, menuitems0_.imagename as imagename1_0_, menuitems0_.sid as sid1_0_, menuitems0_.catid as catid1_0_ from Menu menuitems0_ where menuitems0_.sid=?
Name and version of the database you are using:
MySQL 4.1 Connector/J