DetachedCriteria 使用记录

例子
Newsinfo 一对一关联 NewsInfoView。

Newsinfo


    <class name="com.china.bean.Newsinfo" table="newsinfo" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Long">
            name="id"  precision="22" scale="0" />
            class="native" />
        id>
        <property name="categoryName" type="java.lang.String">
            name="category_name"  length="200"  not-null="true"/>
        property>
        。。。。
        to-one name="newsInfoView" class="com.china.bean.NewsInfoView" cascade="all" lazy="false"/>
    class>

NewsInfoView

 <hibernate-mapping>
    <class name="com.china.bean.NewsInfoView" table="newsinfo_view" >
        <id name="newsId" type="java.lang.Long">
            <column name="news_id" />
            <generator class="foreign" >  
                <param name="property">newsInfoparam>  
            generator>
        id>
        <property name="viewNum" type="java.lang.Long">
            <column name="view_num"   />
        property>
        <one-to-one name="newsInfo" constrained="true">one-to-one>  
    class>
hibernate-mapping>
public class Newsinfo implements Cloneable, java.io.Serializable {
    private Long id;
    private String title;
    。。。。
    private NewsInfoView newsInfoView;  //阅读数

创建

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Newsinfo.class);

条件

等于

 detachedCriteria.add(Restrictions.eq("type", newsinfoSearch.getType()));

相似

detachedCriteria.add(Restrictions.ilike("title", newsinfoSearch.getTitle(), MatchMode.ANYWHERE));

多条件关联

detachedCriteria.add(Restrictions.or(Restrictions.eq("categoryId", newsinfoSearch.getCategoryId()), Restrictions.in("copyToList", caIds)));

排序

正序倒叙

detachedCriteria.addOrder(Order.asc("placeId"));
detachedCriteria.addOrder(Order.desc("newsInfoView"));

关联数据排序

detachedCriteria.createCriteria("newsInfoView",Criteria.LEFT_JOIN).addOrder(Order.desc("viewNum"));

你可能感兴趣的:(数据库,Criteria)