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);
}