package com.ergal.hibernate; import java.util.Iterator; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.orm.hibernate3.HibernateTemplate; publicclass UserDao implements IUserDao ...{ private HibernateTemplate hibernateTemplate; public UserDao()...{} /**//* public UserDao(SessionFactory sessionFactory) { this.sessionFactory=sessionFactory; }*/ publicvoid setSessionFactory(SessionFactory sessionFactory) ...{ hibernateTemplate=new HibernateTemplate(sessionFactory); } publicvoid insert(User user) ...{ hibernateTemplate.save(user); // TODO Auto-generated method stub /**//*Session session = sessionFactory.openSession(); Transaction tx=session.beginTransaction(); session.save(user); tx.commit(); session.close();*/ } public String getPassword(String username) ...{ /**//* Session session = sessionFactory.openSession(); Transaction tx=session.beginTransaction(); List result=session.createQuery("from User u where username=:name").setString("name", username).list(); String password=null; for(Iterator it=result.iterator(); it.hasNext();) { User u =(User)it.next(); Hibernate.initialize(u.getPassword()); password=(String)u.getPassword(); } tx.commit(); session.close(); return password; */ List result=hibernateTemplate.find("from User u where username=?", username); String password=null; for(Iterator it=result.iterator(); it.hasNext();) ...{ User u =(User)it.next(); Hibernate.initialize(u.getPassword()); password=(String)u.getPassword(); } return password; } }
也可以写成
SessionFactory sf=hibernateTemplate.getSessionFactory(); Session session = sf.openSession(); Transaction tx=session.beginTransaction(); List result=session.createQuery("from User u where username=:name").setString("name", username).list(); String password=null; for(Iterator it=result.iterator(); it.hasNext();) ...{ User u =(User)it.next(); Hibernate.initialize(u.getPassword()); password=(String)u.getPassword(); } return password;
convertHibernateAccessException(org.hibernate.HibernateException ex) Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
getHibernateTemplate() Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
protected org.hibernate.Session
getSession() Get a Hibernate Session, either from the current transaction or a new one.
protected org.hibernate.Session
getSession(boolean allowCreate) Get a Hibernate Session, either from the current transaction or a new one.
org.hibernate.SessionFactory
getSessionFactory() Return the Hibernate SessionFactory used by this DAO.
protected void
releaseSession(org.hibernate.Session session) Close the given Hibernate Session, created via this DAO's SessionFactory, if it isn't bound to the thread.
void
setHibernateTemplate(HibernateTemplate hibernateTemplate) Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.
void
setSessionFactory(org.hibernate.SessionFactory sessionFactory) Set the Hibernate SessionFactory to be used by this DAO.
于是可以把代码改成以下来测试一下
package com.ergal.hibernate; import java.util.Iterator; import java.util.List; import org.hibernate.Hibernate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; publicclass UserDao extends HibernateDaoSupport implements IUserDao ...{ //private HibernateTemplate hibernateTemplate; public UserDao()...{} /**//* public UserDao(SessionFactory sessionFactory) { this.sessionFactory=sessionFactory; }*/ /**//* public void setSessionFactory(SessionFactory sessionFactory) { hibernateTemplate=new HibernateTemplate(sessionFactory); }*/ publicvoid insert(User user) ...{ getHibernateTemplate().save(user); // TODO Auto-generated method stub /**//*Session session = sessionFactory.openSession(); Transaction tx=session.beginTransaction(); session.save(user); tx.commit(); session.close();*/ } public String getPassword(String username) ...{ /**//* Session session = sessionFactory.openSession(); Transaction tx=session.beginTransaction(); List result=session.createQuery("from User u where username=:name").setString("name", username).list(); String password=null; for(Iterator it=result.iterator(); it.hasNext();) { User u =(User)it.next(); Hibernate.initialize(u.getPassword()); password=(String)u.getPassword(); } tx.commit(); session.close(); return password; */ List result=getHibernateTemplate().find("from User u where username=?", username); String password=null; for(Iterator it=result.iterator(); it.hasNext();) ...{ User u =(User)it.next(); password=(String)u.getPassword(); } return password; } }
这样很多管理就被省去了 包括session sessionFactory
reference上还写了一些关于使用
Hibernate原生API的方法
就是直接自己编写Hibernate的实现方法来实现DAO
如
Session session = sessionFactory.openSession(); Transaction tx=session.beginTransaction(); List result=session.createQuery("from User u where username=:name").setString("name", username).list(); String password=null; for(Iterator it=result.iterator(); it.hasNext();) ...{ User u =(User)it.next(); Hibernate.initialize(u.getPassword()); password=(String)u.getPassword(); } tx.commit(); session.close(); return password;