Sorted results with Hibernate
So I have a need to get the results from my DB in a sorted list. Hibernate provides a number of ways to do this.
I have implemented it in two ways
I have an Object called Rounds that has a property List
<hibernate-mapping>
<class name="com.willspics.cs.epl.model.rounds.Rounds" table="rounds" catalog="epl" >
<id name="roundId" type="java.lang.Long" >
<column name="round_id" />
<generator class="native"/>
</id>
<property name="deadline" type="java.util.Date" lazy="false">
<column name="deadline" length="0" not-null="true" />
</property>
<property name="minimumPicks" type="java.lang.Long">
<column name="minimum_picks" not-null="true" />
</property>
<set name="games" table="rounds_games" lazy="false" order-by="lower(game_id) asc" cascade="all,delete-orphan">
<key>
<column name="round_id" not-null="true" />
</key>
<many-to-many column="game_id" unique="true" class="com.willspics.cs.epl.model.games.Games" />
</set>
</class>
</hibernate-mapping>
The sorting is done by the order-by=“lower(game_id) asc”. Note that the lower(game_id) asc code is mysql specific and not hql.
The second way is to just use order by gameId in the query with in the Java code.
String queryString = "from Games order by gameId";
Query queryObject = getSession().createQuery(queryString);