Prepared by: Xu jianxiang (netpirate@gmail.com)
Time:
From: http://www.anymobile.org
1. Display by PAGE
Public class QuizRecordDAO extends BaseHibernateDAO
{
Private int count;
Public int getCount ()
{
Return this. count;
}
Public List list (int start, int range)
{
Log. debug ("finding all QuizRecord instance ");
Try
{
Criteria criteria = getSession (). createCriteria (QuizRecord. class );
Count = (Number) criteria. setProjection (Projections. rowCount (). uniqueResult (). intValue ();
Criteria. setProjection (null );
// Criteria. addOrder (Order. desc (TYPE ));
Criteria. setFirstResult (start );
Criteria. setMaxResults (range );
Return criteria. list ();
}
Catch (RuntimeException re)
{
Log. error ("find by property name failed", re );
Throw re;
}
}
Public List list (int awardStatus, int start, int range)
{
Log. debug ("finding all QuizRecord instance ");
Try
{
Criteria criteria = getSession (). createCriteria (QuizRecord. class );
Criteria. add (Expression. eq (AWARD_STATUS, Integer. valueOf (awardStatus )));
Count = (Number) criteria. setProjection (Projections. rowCount (). uniqueResult (). intValue ();
Criteria. setProjection (null );
// Criteria. addOrder (Order. desc (TYPE ));
Criteria. setFirstResult (start );
Criteria. setMaxResults (range );
Return criteria. list ();
}
Catch (RuntimeException re)
{
Log. error ("find by property name failed", re );
Throw re;
}
}
}
2. Foreign key association with non-primary keys
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping file autogenerated by MyEclipse-Hibernate Tools
-->
<Hibernate-mapping>
<Class name = "org. anymobile. admin. entity. QuizRecord" table = "quiz_record" catalog = "fetion">
<Id name = "id" type = "java. lang. Integer">
<Column name = "id"/>
<Generator class = "increment"> </generator>
</Id>
<Your-to-one name = "yangzhouUser" class = "org. anymobile. admin. entity. User" fetch = "select" property-ref = "fetionId">
<Column name = "fetion_id" length = "20" not-null = "true"/>
</Role-to-one>
</Class>
</Hibernate-mapping>
-- Property-ref = "fetionId" is the code manually added <? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping file autogenerated by MyEclipse-Hibernate Tools
-->
<Hibernate-mapping>
<Class name = "org. anymobile. admin. entity. User" table = "f_user" catalog = "fetion">
<Id name = "id" type = "java. lang. Integer">
<Column name = "id"/>
<Generator class = "increment"> </generator>
</Id>
<Property name = "fetionid" type = "Java. Lang. String">
<Column name = "fetion_id" length = "10" not-null = "true" unique = "true"/>
</Property>
<Property name = "phone" type = "Java. Lang. String">
<Column name = "phone" length = "11"/>
</Property>
<Set name = "records" inverse = "true">
<! --
<Key>
<Column name = "fetion_id" length = "15" not-null = "true"/>
</Key> -->
<Key column = "fetion_id" property-ref = "fetionid"/>
<One-to-define class = "org. anymobile. admin. entity. quizrecord"/>
</Set>
</Class>
</Hibernate-mapping>
-- The Green annotation is automatically generated by hibernate. <key column = "fetion_id" property-ref = "fetionid"/> is manually added.