These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Updating related tables - newbie help
PostPosted: Mon Oct 03, 2005 3:18 am 
Newbie

Joined: Sun Oct 02, 2005 9:58 pm
Posts: 7
Location: Brisbane
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 3:22 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
are you committing the transaction?


Top
 Profile  
 
 Post subject: Am I committing?
PostPosted: Mon Oct 03, 2005 3:29 am 
Newbie

Joined: Sun Oct 02, 2005 9:58 pm
Posts: 7
Location: Brisbane
Being male this is hard for me... :-)

I've added some debug lines around my Struts filter to show this happening: the code is:

Code:
>>> CHAIN HERE ************************************************************
>>> CHAIN HERE ************************************************************
>>> CHAIN HERE ************************************************************
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=?
>>> COMMIT HERE ***********************************************************
>>> COMMIT HERE ***********************************************************
>>> COMMIT HERE ***********************************************************
>>> CLOSE HERE ***********************************************************
>>> CLOSE HERE ***********************************************************
>>> CLOSE HERE ***********************************************************


Code:
        try {
            System.err.println(">>> CHAIN HERE ************************************************************");
            System.err.println(">>> CHAIN HERE ************************************************************");
            System.err.println(">>> CHAIN HERE ************************************************************");
            chain.doFilter(request, response);

            // Commit any pending database transaction.
            System.err.println(">>> COMMIT HERE ***********************************************************");
            System.err.println(">>> COMMIT HERE ***********************************************************");
            System.err.println(">>> COMMIT HERE ***********************************************************");
            HibernateUtil.commitTransaction(); 

        } catch (RuntimeException ex) {
            System.err.println(">>> ROLLBACK HERE ***********************************************************");
            System.err.println(">>> ROLLBACK HERE ***********************************************************");
            System.err.println(">>> ROLLBACK HERE ***********************************************************");
            HibernateUtil.rollbackTransaction(); // Also closes the Session 
            // Just rollback and let others handle the exception, e.g. for display
            throw ex;

        } finally {
            System.err.println(">>> CLOSE HERE ***********************************************************");
            System.err.println(">>> CLOSE HERE ***********************************************************");
            System.err.println(">>> CLOSE HERE ***********************************************************");
            // No matter what happens, close the Session.
            HibernateUtil.closeSession();
        }


Looks like the appropriate commit is happening.

Answer, yes.


Top
 Profile  
 
 Post subject: Problem still outstanding - please help
PostPosted: Thu Oct 06, 2005 8:50 pm 
Newbie

Joined: Sun Oct 02, 2005 9:58 pm
Posts: 7
Location: Brisbane
My saveOrUpdate function is still failing to generate any sql...

The code in my action class looks like:

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)) {
            System.err.println(">>> get(" + mf.getSid() + ")");
            SectionForm sf = (SectionForm) session.get(SectionForm.class, new Integer(mf.getSid()));
            System.err.println(">>> mf.sid = " + mf.getSid());
            System.err.println(">>> add menu item " + mf.getTitle() + " to section " + sf.getSection());
            sf.addMenuitem(mf);
            System.err.println(">>> mf.sid = " + mf.getSid());
            System.err.println(">>> update section form...");
            session.saveOrUpdate(sf);
            System.err.println(">>> mf.sid = " + mf.getSid());
            System.err.println(">>> update menu form...");
            session.saveOrUpdate(mf);
            System.err.println(">>> mf.sid = " + mf.getSid());
            System.err.println(">>> done");
        }
       
        // 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");
    }


And the log file at the end of this looks like....

Code:
>>> get(3)
Hibernate: select sectionfor0_.sid as sid0_, sectionfor0_.section as section5_0_, sectionfor0_.seq as seq5_0_ from Sections sectionfor0_ where sectionfor0_.sid=?
>>> mf.sid = 3
>>> add menu item Spare ribs to section Mains
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=?
>>> mf.sid = 3
>>> update section form...
>>> mf.sid = 3
>>> update menu form...
>>> mf.sid = 3
>>> done


Please somebody tell me what I'm doing wrong?

TIA
murray


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.