私はこの Hibernate オブジェクト DAO を作成しましたが、このアプローチでは、セッションごとの更新アプローチを使用しています (これは正しくないと思います)。
遅延フェッチされるコレクションを含む User クラスで問題が発生しているため、私が正しいとは思わない理由。DAO から各ユーザーを取得するとき、セッションは閉じられます。したがって、コレクションを取得できません。
また、オブジェクトが切り離されているため、テーブルに対して不必要な更新が行われることもあります。
getCurrentSession()を使用するなど、DAO を修正する方法はありますか?
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.test.util.DataAccessLayerException;
import org.test.util.HibernateUtil;
public abstract class AbstractDao {
protected Session session;
protected Transaction tx;
public AbstractDao() {
HibernateUtil.buildIfNeeded();
}
protected void saveOrUpdate(Object obj) {
try {
startOperation();
session.saveOrUpdate(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateUtil.close(session);
}
}
protected void delete(Object obj) {
try {
startOperation();
session.delete(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateUtil.close(session);
}
}
protected Object find(Class clazz, Long id) {
Object obj = null;
try {
startOperation();
obj = session.load(clazz, id);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateUtil.close(session);
}
return obj;
}
protected List findAll(Class clazz) {
List objects = null;
try {
startOperation();
Query query = session.createQuery("from " + clazz.getName());
objects = query.list();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateUtil.close(session);
}
return objects;
}
protected void handleException(HibernateException e) throws DataAccessLayerException {
HibernateUtil.rollback(tx);
throw new DataAccessLayerException(e);
}
protected void startOperation() throws HibernateException {
session = HibernateUtil.openSession();
tx = session.beginTransaction();
}
}
HibernateUtil
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);
}
}
}