1、分页显示
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/ 非主键的外键关联
<?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>
<many-to-one name="yangzhouUser" class="org.anymobile.admin.entity.User" fetch="select" property-ref="fetionId">
<column name="fetion_id" length="20" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>-- property-ref="fetionId"是手工添加的代码
<?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-many class="org.anymobile.admin.entity.QuizRecord" />
</set>
</class>
</hibernate-mapping>
--绿色注释部分是Hibernate自动生成的,<key column="fetion_id" property-ref="fetionId"/>是手工添加的。