随笔-1  评论-68  文章-98  trackbacks-0
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"/>是手工添加的。
posted on 2009-04-22 00:02 Xu Jianxiang 阅读(1401) 评论(1)  编辑  收藏 所属分类: Open SourceJava Tech

评论:
# re: Hibernate FAQ(2)--分页显示和非主键的外键关联[未登录] 2011-12-08 11:35 | JOJO
好厉害,解决了我的问题。(非主键)
不知道可否给再我讲讲原理呢,如果可以,请加Q1176792523。或者直接回复我
很感谢!
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: