Hibernate FAQ (2) -- display by PAGE and associate with foreign keys of non-primary keys

Source: Internet
Author: User
Tags rowcount

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.