-->
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: How to sort Detail Table records
PostPosted: Tue May 23, 2006 11:15 pm 
Newbie

Joined: Fri Apr 14, 2006 8:55 am
Posts: 6
Hi,
I am pretty new to Hibernate and needs some help in resolving the following issue. I am trying to retrieve Menu and SubMenus data from
Database tables. I have two tables Hiber_PARENT_MENU and Hiber_CHILD_MENU. How can I keep the order of my detail records i.e. Sub Menus .Right now everytime I refresh the page Sub Menus order keeps changing. Following are the hbm.xml files.


Code:
--------------------------------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="HiberSubMenu" table="Hiber_CHILD_MENU">
      <id name="id" column="SUB_MENU_ID" type="long">
         <generator class="sequence">
            <param name="sequence">Hiber_SUB_MENU_SEQ</param>
         </generator>
      </id>
      <many-to-one name="HiberMenu" class="HiberMenu">
         <column name="MENU_ID"/>
      </many-to-one>
      <property name="subMenuTitle" type="string" length="50" column="SUB_MENU_TITLE"/>
      <property name="subMenuUrl" type="string" length="250" column="SUB_MENU_URL"/>
      <property name="subMenuActive" type="string" length="1" column="SUB_MENU_ACTIVE"/>
      <property name="createdDate" type="timestamp" column="CREATED_DATE"/>
      <property name="createdBy" type="string" length="35" column="CREATED_BY"/>
   </class>
</hibernate-mapping>

------------------------------------------------------------------------


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="HiberMenu" table="Hiber_PARENT_MENU">
      <id name="id" column="MENU_ID" type="long">
         <generator class="sequence">
            <param name="sequence">Hiber_MENU_SEQ</param>
         </generator>
      </id>
      <property name="menuTitle" type="string" length="50" column="MENU_TITLE" not-null="true"/>
      <property name="menuUrl" type="string" length="250" column="MENU_URL" not-null="true"/>
      <property name="menuActive" type="string" length="1" column="MENU_ACTIVE" not-null="true"/>
      <property name="subMenuFlag" type="string" length="1" column="SUB_MENU_FLAG" not-null="true"/>
      <property name="createdDate" type="timestamp" column="CREATED_DATE" not-null="true"/>
      <property name="createdBy" type="string" length="35" column="CREATED_BY" not-null="true"/>
      <set name="subMenu" inverse="true" cascade="all">
         <key>
            <column name="MENU_ID"/>
         </key>
         <one-to-many class="HiberSubMenu"/>
      </set>
   </class>
</hibernate-mapping>


Here is the code that gets the data:

Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();

Code:
                     
List result = session.createQuery("from HiberMenu as hibermenu where hibermenu.menuHolder = 'Genaral'").list();



Any help is appreciated. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 11:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
<set> takes an order-by attribute and/or a sort attribute.

order-by is the easy one. It takes plain old SQL: usually the name of a column, often followed by ASC|DESC, but you can put in subselects, surrounded by parentheses, if you like.

sort has a few values. Obviously natural (the default) isn't going to work for you, unless you make your submenu java class implement Comparable. The alternative is to write a Comparator class, and give its name to sort.

Both attributes are described in the ref docs, section 7.2, Collection Mappings.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 9:37 am 
Newbie

Joined: Fri Apr 14, 2006 8:55 am
Posts: 6
I don't have an inner select explicitly on Deatil table. I am just using the following to get all the Master and Detail table, HiberMenu is my master table:

Code:
from HiberMenu as hibermenu where hibermenu.menuHolder = 'Genaral'").list()


I didn't find much in the docs. how can I modify my mapping to include order-by on the set. Any small example will help a lot. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 5:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Perhaps I can refer you to my previous post. It told you exactly what you can do, and where in the refdocs you can look for more info.

In your MAPPING, you specify a sort order, using one of the two mentioned attributes. In you HQL, you do nothing: the mapping's ordering will be applied.

_________________
Code tags are your friend. Know them and use them.


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.