Java Hibernate打开/关闭会话,DAO的正确方法
我已经编写了这个Hibernate对象DAO,但是通过这种方法,它使用了每次更新会话的方法(我认为这是不对的) 我认为这是不对的,因为我的用户类遇到了问题,其中包含了延迟获取的集合。因为当从DAO检索每个用户时,会话是关闭的。因此,我无法获得我的收藏 由于对象已分离,因此它还不时对表进行大量不必要的更新 有没有办法修复我的DAO,比如使用getCurrentSession() HibernateUtilJava Hibernate打开/关闭会话,DAO的正确方法,java,hibernate,session,dao,sessionfactory,Java,Hibernate,Session,Dao,Sessionfactory,我已经编写了这个Hibernate对象DAO,但是通过这种方法,它使用了每次更新会话的方法(我认为这是不对的) 我认为这是不对的,因为我的用户类遇到了问题,其中包含了延迟获取的集合。因为当从DAO检索每个用户时,会话是关闭的。因此,我无法获得我的收藏 由于对象已分离,因此它还不时对表进行大量不必要的更新 有没有办法修复我的DAO,比如使用getCurrentSession() HibernateUtil import org.apache.commons.logging.Log; import
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;
private static SessionFactory configureSessionFactory()
throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
return sessionFactory;
}
public static SessionFactory buildIfNeeded()
throws DataAccessLayerException {
if (sessionFactory != null) {
return sessionFactory;
}
try {
return configureSessionFactory();
} catch (HibernateException e) {
throw new DataAccessLayerException(e);
}
}
public static SessionFactory buildSessionFactory()
throws HibernateException {
if (sessionFactory != null) {
closeFactory();
}
return configureSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session openSession() throws HibernateException {
buildIfNeeded();
return sessionFactory.openSession();
}
public static void closeFactory() {
if (sessionFactory != null) {
try {
sessionFactory.close();
} catch (HibernateException ignored) {
log.error("Couldn't close SessionFactory", ignored);
}
}
}
public static void close(Session session) {
if (session != null) {
try {
session.close();
} catch (HibernateException ignored) {
log.error("Couldn't close Session", ignored);
}
}
}
public static void rollback(Transaction tx) {
try {
if (tx != null) {
tx.rollback();
}
} catch (HibernateException ignored) {
log.error("Couldn't rollback Transaction", ignored);
}
}
}
您可以在HibernateUtil中保存静态会话成员。懒惰初始化。
可以随时关闭会话,但在会话未关闭之前,您将继续使用它。好的方法是将close方法添加到DAO(AbstractDao)中,并将其称为“工作单元”的结束 请不要对会话进行静态引用,会话不是线程安全的
下面是一个精彩的示例解释:但这样我将只使用一个会话。这是需要的吗?当您使用startOperation()获取新会话时,您可以使用closeOperation()关闭会话,并随时使用。如果将会话设置为静态,则此交互模式不是线程安全的。
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;
private static SessionFactory configureSessionFactory()
throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
return sessionFactory;
}
public static SessionFactory buildIfNeeded()
throws DataAccessLayerException {
if (sessionFactory != null) {
return sessionFactory;
}
try {
return configureSessionFactory();
} catch (HibernateException e) {
throw new DataAccessLayerException(e);
}
}
public static SessionFactory buildSessionFactory()
throws HibernateException {
if (sessionFactory != null) {
closeFactory();
}
return configureSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session openSession() throws HibernateException {
buildIfNeeded();
return sessionFactory.openSession();
}
public static void closeFactory() {
if (sessionFactory != null) {
try {
sessionFactory.close();
} catch (HibernateException ignored) {
log.error("Couldn't close SessionFactory", ignored);
}
}
}
public static void close(Session session) {
if (session != null) {
try {
session.close();
} catch (HibernateException ignored) {
log.error("Couldn't close Session", ignored);
}
}
}
public static void rollback(Transaction tx) {
try {
if (tx != null) {
tx.rollback();
}
} catch (HibernateException ignored) {
log.error("Couldn't rollback Transaction", ignored);
}
}
}