Hibernate 分页显示和非主键的外键关联

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"/>是手工添加的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值