Generic Data Access ObjectsOrdinary data Access object, this is a DAO class design pattern above the official website of Hibernate, based on JDK5.0 type support, the article address is as follows: http://www.hibernate.org/328.html My following code is a little different from what the Hibernate official Web site offers.
First defines the DAO class's interface Igenericdao, which defines common CRUD operations: Java code /** * defines generic crud operations * @author rainlife */ public Interface igenericdao〈t, ID extends serializable〉{ //& nbsp; finds an object by its primary key identity. public t findbyid (id id); // to find an object by its primary key identity, you can lock the corresponding record in the table. t findbyid (id id, boolean lock); //get all the objects. list findall (); // Finds the object that matches it by a given object. list findbyexample (t exampleinstance); //the persisted object. t makepersistent (t entity); //deletes an object. void maketransient (t entity); } The following is the implementation of the interface using Hibernate Genericdaohibernate:java code /** * This is the hibernate implementation of the Igenericdao interface, Complete the common CRUD operations. * @author rainlife * @param pojo class * @param pojo class's primary key identifier * @param Implementation of the DAO class for each Pojo class */ Public abstract class genericdaohibernate〈t,id Extends serializable, daoimpl extends IGenericDAO〈T,ID〉〉 implements igenericdao〈t,id〉{ private Class persistentClass; protected Session session; public genericdaohibernate () { this.persistentclass = (Class) ((Parameterizedtype) getclass () .getgenericsuperclass ()). Getactualtypearguments () [0]; } @SuppressWarnings ("unchecked") public daoimpl setsession ( Session s) { this.session = s; return ( Daoimpl) this; } protected session getsession () { if (session == null) throw new illegalstateexception ( "Session has not been set on dao before usage "); return session; } public class getpersistentclass () { return persistentClass; } @SuppressWarnings ("unchecked") public t findbyid (id id) { return (T) getsession (). Load ( Getpersistentclass (), &Nbsp;id); } @SuppressWarnings ("unchecked") public t findbyid (ID id, boolean lock) { T entity; if ( Lock) entity = (T ) getsession (). Load (Getpersistentclass (), id, lockmode.upgrade); else entity = findbyid (ID); return entity; } @SuppressWarnIngs ("unchecked") public list findall () { return findbycriteria (); } @SuppressWarnings ("unchecked") public list findbyexample (t exampleinstance) { criteria crit = getsession (). Createcriteria (Getpersistentclass ()); example example = example.create (exampleinstance); crit.add (example); return crit.list (); } @SuppressWarnings ("unchecked") public list findbyexample (t exampleinstance, string[] excludeproperty) { criteria crit = getsession (). CreateCriteria ( Getpersistentclass ()); example example = example.create (exampleinstance); for ( String exclude : excludeproperty) { example.excludeproperty ( Exclude); } crit.add (example); return crit.list (); } @SuppressWarnings (" Unchecked ") public t makepersistent (t entity) { getsession (). saveorupdate (entity); //getsession (). Save (entity); return entity; } public void maketransient (t entity) { getsession (). Delete (entity); } @SuppressWarnings (" Unchecked ") protected list findbycriteria (criterion... Criterion) { criteria crit = getsession (). Createcriteria (Getpersistentclass ()); for (criterion c : criterion) { Crit.add (c); } return crit.list (); } @SuppressWarnings ("unchecked") /** * added to the sorting function. */ protected List Findbycriteria (order order,criterion... criterion) { criteria crit = getsession (). Createcriteria (Getpersistentclass ()); for (criterion c : criterion) { crit.add (c); } if (order!=null) Crit.addorder (Order); return crit.list (); } @ Suppresswarnings ("unchecked") protected list findbycriteria (int firstresult,int rowcount,order order,criterion... criterion) { criteria crit = getsession (). Createcriteria (Getpersistentclass ()); for ( criterion c : criterion) { crit.add (c); } if ( Order!=null) crit.addorder ( Order); crit.setfirstresult (FirstResult); crit.setmaxresults (ROWCOUNT); return crit.list (); } } So, we're going to use the DAO class, You can inherit directly from this hibernate DAO Class: For example, we define an Iuserdao interface that inherits Igenericdao:java code public interface iuserdao Extends igenericdao〈user,integer〉{ public user find (String Username,string password); public user find (String username); }
This interface inherits from Igenericdao and naturally defines the generic CRUD operations defined by the Igenericdao interface. Let's take a look at the hibernate implementation for Iuserdao Userdaohibernate:java code public class Userdaohibernate extends Genericdaohibernate〈user, Integer,iuserdao〉implements Iuserdao {
Public user find (String username, string password) { //here omit specific code } public user find (string username) { //here omit specific code } } Userdaohibernate inherits Genericdaohibernate and implements the Iuserdao interface, so that our userdaohibernate has both common crud operations and specific business operations for the user.