-->
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.  [ 3 posts ] 
Author Message
 Post subject: Retrieving and sorting collection using Criteria/HQL?
PostPosted: Wed Mar 31, 2004 7:47 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hi what I would like to do is retrieve a collection of files from a SPECIFIC FileType object sorted using fileName or any of the other parameters inside the File class mapping. Can this be done using Criteria or HQL? I tried specifing order-by on the set in the FileType but that failed. It is mapped as a many-to-many, from FileType to File. Here is the mapping info:

Code:
<hibernate-mapping>
    <class name="FileType" table="fileType">
      <id name="id" type="long" unsaved-value="null">
           <generator class="identity"/>
        </id>

        <set name="files" cascade="none" lazy="true" table="fileType_file">
            <key>
                <column name="type_id" not-null="true"/>
            </key>
            <many-to-many class="File">
                <column name="file_id" not-null="true"/>
            </many-to-many>
        </set>

        <set name="fileAttributeTypes" cascade="none" lazy="true" table="fileType_fileAttributeType">
            <key>
                <column name="type_id" not-null="true"/>
            </key>
            <many-to-many class="FileAttributeType">
                <column name="item_id" not-null="true"/>
            </many-to-many>
        </set>
        <property name="name"/>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="File" table="file">
      <id name="id" type="long" unsaved-value="null">
           <generator class="identity"/>
        </id>
        <property name="date"/>
        <property name="description">
           <column name="description" sql-type="mediumtext"/>           
        </property>
        <many-to-one name="fileData" outer-join="false" cascade="all" column="fileData_id" not-null="true" unique="true"/>
        <property name="fileName"/>
        <set name="fileAttributes" cascade="all-delete-orphan" lazy="true">
         <key column="file_id"/>
         <one-to-many class="FileAttribute"/>
        </set>
        <property name="length"/>
        <property name="title"/>
    </class>
</hibernate-mapping>



Any help is greatly appreciated!!
-David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 8:31 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Ok figured out how to do this using HQL... would be great if someone can post how to accomplish this using Criteria though.

Code:
select elements(ft.files) from FileType as ft where ft.id = :FTID order by fileName desc


-David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 7:37 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
I believe that Criteria API allows you do this by define a secondary criteria on the Collection class. You then apply your restrictions, order and other desired effects on this secondary criteria.

Code:
   class Main {
      List<Things> things;
      // more stuff in here
   }

   Criteria main = session.createCriteria(Main.class);
   Criteria secondary = main.createCriteria("things");
   secondary.addOrder(Order.asc("beauty"));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.