getHibernateTemplate().save(user);
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
HibernateUtil.colseSession();
package kick.hibernate;
import.net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
public class HibernateTemplate{
public static Object run(HibernateCallback callback) throws HibernateException{
Session session = null;
Transaction tx = null;
try {
session = HibernateSessionutil.currentSession();
tx = session.beginTransaction();
Object result = callback.execute(session);
tx.commit();
session.flush();
return result;
} catch (HibernateException e) {
tx.rollback();
return null;
} finally {
HibernateSessionutil.closeSession();
}
}
package kick.hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
public interface HibernateCallBack {
Object execute(Session session)throws HibernateException;
}
HibernateTemplate.run(new HibernateCallback() {
public Object execute(Session session) throws HibernateException {
session.save(user);
return null;
}
});
package kick.hibernate.dao;
import net.sf.hibernate.Session;
import kick.hibernate.HibernateTemplateImpl;
public abstract class RootDao {
private HibernateSupport temp = null;
/**
* @return Returns the temp.
*/
public HibernateTemplateImpl getHibernateTemplate(Session session) {
return new HibernateSupport();
}
}
public class UserDaoImpl extends RootDao implements UserDaoInterface{
public void saveUser(User user) throws KickException {
getHibernateTemplate().saveOrUpdate(user);
}
…………………………………………
实现其他的方法
…………………………………………
}