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(); } }); }