`
Clayz
  • 浏览: 298542 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

DetachedCriteria,Hibernate模糊查询

阅读更多

1,假设存在Thesis对象:

package org.cms.po;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Thesis generated by MyEclipse Persistence Tools
 */

public class Thesis implements java.io.Serializable {
	// Fields
	private Integer thesisid;
	private Project project;
	private User userByReader;
	private Type type;
	private User userByUploader;
	private String name;
	private String author;
	private String source;
	private Date publishdate;
	private Date uploadtime;
	private String summary;
	private Integer downloadtimes;
	private Integer status;
	private String remark;
	private String attachmentlink;
	private Integer responseamount;
	private Set responses = new HashSet(0);
	private Set attachments = new HashSet(0);

	// Constructors

	/** default constructor */
	public Thesis() {}

	/** minimal constructor */
	public Thesis(Integer thesisid, Project project, Type type, String name, String attachmentlink,
 Date uploadtime, Integer status) {
		this.thesisid = thesisid;
		this.project = project;
		this.type = type;
		this.name = name;
		this.uploadtime = uploadtime;
		this.status = status;
		this.attachmentlink = attachmentlink;
	}

	/** full constructor */
	public Thesis(Integer thesisid, Project project, User userByReader, Type type,
 User userByUploader, String name, String author, String source, Date publishdate,
 String attachmentlink, Date uploadtime, String summary, Integer downloadtimes,
 Integer status, Integer responseamount, String remark, Set responses, Set attachments)  {
		this.thesisid = thesisid;
		this.project = project;
		this.userByReader = userByReader;
		this.type = type;
		this.userByUploader = userByUploader;
		this.name = name;
		this.author = author;
		this.source = source;
		this.publishdate = publishdate;
		this.uploadtime = uploadtime;
		this.summary = summary;
		this.downloadtimes = downloadtimes;
		this.status = status;
		this.remark = remark;
		this.attachmentlink = attachmentlink;
		this.responseamount = responseamount;
		this.responses = responses;
		this.attachments = attachments;
	}

	// Property accessors
	public Integer getThesisid() {
		return this.thesisid;
	}

	public void setThesisid(Integer thesisid) {
		this.thesisid = thesisid;
	}

	public Project getProject() {
		return this.project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	......
 
}

 

2,其映射文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!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 Persistence Tools
-->
<hibernate-mapping>
    <class name="org.cms.po.Thesis" table="thesis" catalog="cms">
        <id name="thesisid" type="java.lang.Integer">
            <column name="THESISID" />
            <generator class="native" />
        </id>
        <many-to-one name="project" class="org.cms.po.Project" fetch="select" lazy="false">
            <column name="PROJECTID" not-null="true"/>
        </many-to-one>
        <many-to-one name="userByReader" class="org.cms.po.User" fetch="select" lazy="false">
            <column name="READER"/>
        </many-to-one>
        <many-to-one name="type" class="org.cms.po.Type" fetch="select" lazy="false">
            <column name="TYPE" not-null="true"/>
        </many-to-one>
        <many-to-one name="userByUploader" class="org.cms.po.User" fetch="select" lazy="false">
            <column name="UPLOADER"/>
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="100" not-null="true" />
        </property>
        <property name="author" type="java.lang.String">
            <column name="AUTHOR" length="100"/>
        </property>
        <property name="source" type="java.lang.String">
            <column name="SOURCE" length="100"/>
        </property>
        <property name="publishdate" type="java.util.Date">
            <column name="PUBLISHDATE" length="0"/>
        </property>
        <property name="uploadtime" type="java.util.Date">
            <column name="UPLOADTIME" length="0" not-null="true"/>
        </property>
        <property name="summary" type="java.lang.String">
            <column name="SUMMARY" length="65535"/>
        </property>
        <property name="downloadtimes" type="java.lang.Integer">
            <column name="DOWNLOADTIMES"/>
        </property>
        <property name="responseamount" type="java.lang.Integer">
            <column name="RESPONSEAMOUNT" />
        </property>
        <property name="status" type="java.lang.Integer">
            <column name="STATUS" not-null="true"/>
        </property>
        <property name="remark" type="java.lang.String">
            <column name="REMARK" length="65535" />
        </property>
        <property name="attachmentlink" type="java.lang.String">
            <column name="ATTACHMENTLINK" length="20" not-null="true"/>
        </property>
        <set name="responses" inverse="true">
            <key>
                <column name="THESISID" not-null="true"/>
            </key>
            <one-to-many class="org.cms.po.Response" />
        </set>
        <set name="attachments" inverse="true">
            <key>
                <column name="THESISID" not-null="true" />
            </key>
            <one-to-many class="org.cms.po.Attachment" />
        </set>
    </class>
</hibernate-mapping>

 

3,部分属性进行模糊查询,生成DetachedCriteria查询对象:

public List<Thesis> queryThesis(Map queryInfo) throws ExceptionHandle {
	try {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Thesis.class, "t");
		if (queryInfo.get("name") != null) {
			detachedCriteria.add(Restrictions.like("t.name", "%" + queryInfo.get("name") + "%"));
		}

		if (queryInfo.get("typeid") != null && !queryInfo.get("typeid").toString().equals("0")) {
			detachedCriteria.createAlias("type", "tp").add(Restrictions.eq("tp.typeid", Integer.parseInt(queryInfo.get("typeid").toString())));
		}

		if (queryInfo.get("projectid") != null && !queryInfo.get("projectid").toString().equals("0")) {
			detachedCriteria.createAlias("project", "p").add(Restrictions.eq("p.projectid", Integer.parseInt(queryInfo.get("projectid").toString())));
		}

		if (queryInfo.get("teacherid") != null && !queryInfo.get("teacherid").toString().equals("0")) {
			detachedCriteria.createAlias("userByUploader", "u").add( Restrictions.eq("u.userid", Integer.parseInt(queryInfo.get("teacherid").toString())));
		}

		if (queryInfo.get("author") != null) {
			detachedCriteria.add(Restrictions.like("t.author", "%" + queryInfo.get("author") + "%"));
		}

		if (queryInfo.get("source") != null) {
			detachedCriteria.add(Restrictions.like("t.source", "%" + queryInfo.get("source") + "%"));
		}

		if (queryInfo.get("dateBegin") != null && queryInfo.get("dateEnd") != null) {
			detachedCriteria.add(Restrictions.between("t.uploadtime", (Date) queryInfo.get("dateBegin"), (Date) queryInfo.get("dateEnd")));
		}

		List list = getDaoManager().getThesisDao().findByCriteria(detachedCriteria);
		if (list != null && list.size() != 0) {
			return list;
		}
	} catch (Exception e) {
		throw new ExceptionHandle(e, "查询文献异常");
	}

	return null;

}

 

4,DAO部分:

public List findByCriteria(final DetachedCriteria detachedCriteria) {
	return (List) getHibernateTemplate().execute(new HibernateCallback() {
		public Object doInHibernate(Session session) throws HibernateException {
			Criteria criteria = detachedCriteria.getExecutableCriteria(session);
			return criteria.list();
		}
	});
}
 
分享到:
评论

相关推荐

    Nhibernate的模糊查询

    对于Nhibernate不直接支持的模糊查询,可以编写自定义的HQL(Hibernate Query Language)来实现。 总结,Nhibernate的模糊查询提供了灵活的API,可方便地在C#代码中完成SQL的`LIKE`操作。然而,使用时要注意性能...

    Hibernate的Criteria用法总结

    Criteria用于在线查询,直接与Hibernate Session关联,而DetachedCriteria则可在不依赖Session的情况下创建,适用于离线或延迟执行的查询。DetachedCriteria可以通过`forClass(Class)`或`forEntityName(String)`静态...

    Hibernate中Criteria的用法详解

    通过Example.create()方法,你可以忽略大小写,启用模糊匹配等,这在处理基于对象属性的查询时非常有用。 Projection接口用于查询结果的投影,即指定查询返回的字段。SimpleProjection如avg(), count(), max(), min...

    Hibernate中Criteria的完整用法

    你可以忽略大小写,设置模糊匹配等,使得查询更加灵活。 **Projection** 是用于报表查询和聚合操作的接口。它定义了如何从数据库中提取数据,例如平均值(avg)、计数(count)、最大值(max)、最小值(min)和...

    Hibernate的Criteria文档

    此外,`Example`类允许根据一个对象实例创建查询条件,这对于基于对象属性的模糊匹配非常有用。 `Projection`则用于构建报表查询,它可以获取查询结果的聚合信息,如平均值、计数、最大值、最小值或求和。`...

    Hibernate Criteria 完全使用

    Hibernate Criteria API 是 Hibernate 框架中一个强大的查询工具,它提供了一种面向对象的方式来构建SQL查询,使得开发者无需直接编写SQL语句,即可进行复杂的数据库操作。Criteria API 提供了高度抽象的接口,可以...

    Hibernate中Criteria的完整用法.docx

    本文将详细介绍Hibernate中的Criteria API及其子类DetachedCriteria的使用方法,以及如何利用它们进行高效的数据查询。 #### 二、Criteria与DetachedCriteria ##### 1. Criteria - **定义**:`Criteria` 是...

    Hibernate的Criteria用法

    Hibernate 3引入了`DetachedCriteria`,它允许你在不直接涉及Session的情况下构建查询条件。这有助于分离业务逻辑和数据访问层,使代码更易管理。创建DetachedCriteria的示例如下: ```java DetachedCriteria ...

    Hibernate中Criteria的用法

    而在Hibernate提供的多种查询方式中,Criteria API是一种非常灵活且强大的查询工具,它支持复杂的条件组合以及分组、排序等功能,为开发者提供了更为方便的查询手段。 #### 二、Criteria简介 Criteria是Hibernate...

    Hibernate中Criteria的使用

    2. **限制(Restrictions)**:这是添加查询条件的方式,例如等值比较、范围比较、模糊查询等。例如,查找名字为"John"的用户: ```java criteria.add(Restrictions.eq("name", "John")); ``` 3. **排序(Order...

    Hibernate学习资料

    通过深入探讨Hibernate的Criteria API和`Restrictions`的使用,我们不仅掌握了如何构建复杂查询的能力,还了解了如何利用`DetachedCriteria`提高代码的可维护性和灵活性。这些技术在日常开发中极为实用,能够显著...

    Criteria.pdf

    ### Criteria与Hibernate模糊查询 在Hibernate框架中,`Criteria`接口是执行动态SQL查询的关键工具之一,它允许开发者以编程的方式构建复杂的查询条件,而无需直接编写SQL语句。这在处理复杂的数据检索需求时尤为...

    Criteria的完整用法.pdf

    在基于Spring和Hibernate的项目开发中,Criteria API提供了灵活且强大的查询功能,使开发者能够轻松构建复杂的查询条件,从而提高数据检索的效率与精确度。本文将深入探讨Hibernate中Criteria API的全面应用,包括其...

Global site tag (gtag.js) - Google Analytics