-->
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.  [ 1 post ] 
Author Message
 Post subject: Help with criteria queries...
PostPosted: Mon Apr 18, 2005 11:56 pm 
Beginner
Beginner

Joined: Fri Dec 10, 2004 11:46 pm
Posts: 37
So far I have managed to set up a simple criteria query, that allows me to order my results by asc or desc and to return a maximum of results. This all works fine, but now I would like to add a criteria, from one of my joined tables.

Bassically I want to return my top 5 artists ordered by artistId, where the principal is active (true). I have included the portion of my mapping file and the code am trying to run...

Am not sure how the Example class works and if someone can show me here.

Thanks

Hibernate version: 2.17

Mapping documents:

Code:

   <class name="org.mamoth.Principal" table="Principals">
      <id name="principalID" column="PrincipalID" type="int" unsaved-value="0">
              <generator class="identity" />
      </id>

      <timestamp  name="dateRegistered" column="DateRegistered"/>

      <property name="principal" type="java.lang.String">
         <column name="Principal" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="password" type="java.lang.String">
         <column name="Password" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="uuid" type="java.lang.String">
         <column name="UUID" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="active" type="boolean">
         <column name="Active" sql-type="bit"/>
      </property>
      

   </class>

   <class name="org.mamoth.Artist" table="Artists">
      <id name="artistID" column="ArtistID" type="int" unsaved-value="0">
              <generator class="identity" />
      </id>

      <timestamp  name="dateCreated" column="DateCreated"/>

      <property name="principalName" type="java.lang.String" insert="false" update="false">
         <column name="Principal" sql-type="varchar" length = "50" not-null="true"/>
      </property>
      <property name="name" type="java.lang.String">
         <column name="Name" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="address1" type="java.lang.String">
         <column name="Address1" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="address2" type="java.lang.String">
         <column name="Address2" sql-type="varchar" length="50" not-null="false"/>
      </property>
      <property name="city" type="java.lang.String">
         <column name="City" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="state" type="java.lang.String">
         <column name="State" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="countryID" type="int" insert="false" update="false">
         <column name="CountryID" sql-type="integer" not-null="true"/>
      </property>
      <property name="postalCode" type="java.lang.String">
         <column name="PostalCode" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="genreID" type="int" insert="false" update="false">
         <column name="GenreID" sql-type="integer" not-null="true"/>
      </property>
      <property name="bookingEmail" type="java.lang.String">
         <column name="BookingEmail" sql-type="varchar" length="50" not-null="false"/>
      </property>
      <property name="webSiteURL" type="java.lang.String">
         <column name="WebSiteURL" sql-type="varchar" length="100" not-null="false"/>
      </property>
      <property name="bio" type="java.lang.String">
         <column name="Bio" sql-type="varchar" length="1000" not-null="false"/>
      </property>
      <property name="picture" type="java.lang.String">
         <column name="Picture" sql-type="varchar" length="50" not-null="false"/>
      </property>
      <property name="pictureThumb" type="java.lang.String">
         <column name="PictureThumb" sql-type="varchar" length="50" not-null="false"/>
      </property>
      <property name="planID" type="int" insert="false" update="false">
         <column name="PlanID" sql-type="integer" not-null="true"/>
      </property>

      <many-to-one name="principal" property-ref="principal" class="org.mamoth.Principal"/>

      <many-to-one name="country" column="CountryID" class="org.mamoth.Country"/>

      <many-to-one name="genre" column="GenreID" class="org.mamoth.Genre"/>

      <many-to-one name="plan" column="PlanID" class="org.mamoth.ArtistPlan"/>

   </class>


Code:
    public List getArtistListTOPASC(int maxResults)
    {
        List artistList = new ArrayList();
       
        try
      {
            Session hSession = HibernateSessionFactory.currentSession();
            HibernateSessionFactory.beginTransaction();
           
            artistList = hSession.createCriteria(Artist.class)
            .addOrder(Order.asc("artistID")).setMaxResults(maxResults)
            .list();

            HibernateSessionFactory.commitTransaction();
        }
        catch (HibernateException e)
      {
            HibernateSessionFactory.rollbackTransaction();
           
            e.printStackTrace();
        }
        finally
      {
            HibernateSessionFactory.closeSession();
        }
       
        return(artistList);
    }


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

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.