-->
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: Using criteria queries?
PostPosted: Tue Apr 19, 2005 10:42 pm 
Beginner
Beginner

Joined: Fri Dec 10, 2004 11:46 pm
Posts: 37
Hi I have to persitant classes...

1- Artist
2- Principal

The classes are linked by many-to-one using the "principal" column...

Now I would like to get a list of top X artists only if their principal account is active.

So far I have this which retrieves top X artists.

List artistList = hSession.createCriteria(Artist.class) .addOrder(Order.asc("artistID")).setMaxResults(maxResults) .list();

Now from what i read in the docs, I need to use an Example with an instance of my Pricnipal class. Problem is I dont have an instance of a principal, because I want to get all artist which have the Principal accounst active...

Here is the mapping doc. Am using hibernate 2.1.7 Thanks

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

   <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.Role" table="Roles">
      <id name="roleID" column="RoleID" type="int" unsaved-value="0">
              <generator class="identity" />
      </id>
      <property name="role" type="java.lang.String">
         <column name="Role" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="roleGroup" type="java.lang.String">
         <column name="RoleGroup" sql-type="varchar" length="50" not-null="false"/>
      </property>
      
      <many-to-one name="principal" class="org.mamoth.Principal" property-ref="principal"/>
   </class>

   <class name="org.mamoth.Country" table="Countries">
      <id name="countryID" column="CountryID" type="int">
              <generator class="identity" />
      </id>
      <property name="country" type="java.lang.String">
         <column name="Country" sql-type="varchar" length = "50" not-null="true"/>
      </property>
      <property name="code" type="java.lang.String">
         <column name="Code" sql-type="char" length="2" not-null="true"/>
      </property>      
   </class>

   <class name="org.mamoth.Genre" table="Genres">
      <id name="genreID" column="GenreID" type="int" unsaved-value="0">
              <generator class="assigned" />
      </id>
      <property name="genre" type="java.lang.String">
         <column name="Genre" sql-type="varchar" length = "25" not-null="true"/>
      </property>
      <property name="description" type="java.lang.String">
         <column name="Description" sql-type="varchar" length="100" not-null="false"/>
      </property>      
   </class>

   <class name="org.mamoth.ArtistPlan" table="ArtistPlans">
      <id name="planID" column="PlanID" type="int">
              <generator class="identity" />
      </id>

      <property name="totalBytesAllowed" type="int">
         <column name="TotalBytesAllowed" sql-type="integer"/>
      </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>

   <class name="org.mamoth.Music" table="Music">
      <id name="musicID" column="MusicID" type="int" unsaved-value="0">
              <generator class="identity" />
      </id>
      <property name="artistID" type="int" insert="false" update="false">
         <column name="ArtistID" sql-type="integer" not-null="true"/>
      </property>
      <property name="title" type="java.lang.String">
         <column name="Title" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="year" type="java.lang.String">
         <column name="Year" sql-type="varchar" length="4" not-null="true"/>
      </property>
      <property name="length" type="java.lang.String">
         <column name="Length" sql-type="varchar" length="10" not-null="true"/>
      </property>
      <property name="bitrate" type="java.lang.String">
         <column name="Bitrate" sql-type="varchar" length="10" not-null="true"/>
      </property>
      <property name="file" type="java.lang.String">
         <column name="File" sql-type="varchar" length="50" not-null="true"/>
      </property>
      <property name="fileSize" type="int">
         <column name="FileSize" sql-type="integer" not-null="true"/>
      </property>
      
      <many-to-one name="artist" column="ArtistID" class="org.mamoth.Artist"/>

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

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Using criteria queries?
PostPosted: Tue Apr 19, 2005 11:12 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
infectedrhythms wrote:
Hi I have to persitant classes...

1- Artist
2- Principal

The classes are linked by many-to-one using the "principal" column...

Now I would like to get a list of top X artists only if their principal account is active.

So far I have this which retrieves top X artists.

List artistList = hSession.createCriteria(Artist.class) .addOrder(Order.asc("artistID")).setMaxResults(maxResults) .list();

Now from what i read in the docs, I need to use an Example with an instance of my Pricnipal class. Problem is I dont have an instance of a principal, because I want to get all artist which have the Principal accounst active...

Here is the mapping doc. Am using hibernate 2.1.7 Thanks


Look at the Expression class in the documentation.

You can do something like
Code:
hSession.createCriteria(Artist.class)
             .add(Expression.eq("princpal.active",Boolean.TRUE))
             .addOrder(Order.asc("artistID"))
             .setMaxResults(maxResults)   
             .list()


Top
 Profile  
 
 Post subject: Re: Using criteria queries?
PostPosted: Tue Apr 19, 2005 11:15 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pksiv wrote:
You can do something like
Code:
hSession.createCriteria(Artist.class)
             .add(Expression.eq("princpal.active",Boolean.TRUE))
             .addOrder(Order.asc("artistID"))
             .setMaxResults(maxResults)   
             .list()


Actually, you'll also need to create an alias for princpal.
Code:
hSession.createCriteria(Artist.class)
             .createAlias("principal","principal")
             .add(Expression.eq("principal.active",Boolean.TRUE))
             .addOrder(Order.asc("artistID"))
             .setMaxResults(maxResults)   
             .list()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 8:40 am 
Beginner
Beginner

Joined: Fri Dec 10, 2004 11:46 pm
Posts: 37
Cool, Ill try it out and let you know!


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.