我在使用Hibernate 4.2的Web应用程序中工作,我实现了抽象工厂模式但是为了创建会话工厂我没有init方法因为我使用第一次调用来创建会话工厂以重用像静态一样前进,就像你可以看到我从DAOImpl获得会话,工作,最后我关闭它。首先,我想问一下这种做法是否正确?还有其他更好的做法吗?
public class HibernateSessionFactory {
private static SessionFactory sessionFactory = buildSessionFactory();
private static final Logger logger = LogManager.getLogger(HibernateSessionFactory.class);
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sf = configuration.buildSessionFactory(sr);
return sf;
} catch (Exception ex) {
logger.error("Initial SessionFactory creation failed.", ex);
ex.printStackTrace();
throw ex;
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdownSessionFactory() {
sessionFactory.close();
}
}
DAOIMPL
public class RecordDAOImpl implements RecordDAO{.....
public String getRecord (Long id){
Session session = HibernateSessionFactory.getSessionFactory().openSession();
try {
session.beginTransaction();
.
.
.
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
return value;
}
DAO
public interface FieldDAO {.....
String getRecord (Long id);
工厂
public class HibernateFactoryDao extends DAOFactory {...
@Override
public RecordDAOImpl getRecordDAO() {
return new RecordDAOImpl();
}
答案 0 :(得分:0)
作为替代方案,您可以使用CDI将工厂注入RecordDAOImpl
并根据您的ORM类型检索您的会话。
作为一个可能的例子,您可以尝试以下方法:
1。在您的ORM工厂之间创建一个限定符
@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER })
public @interface ORMSessionFactory {
ORMType value();
public enum ORMType {
HIBERNATE,
ECLIPSELINK;
}
}
2. 为会话工厂定义界面
public interface MySessionFactory {
public SessionFactory getSessionFactory();
public void shutdownSessionFactory();
}
3. 如有必要,创建一个抽象类,其中包含所有ORM工厂实现中的常见行为
public abstract class AbstractSessionFactory implements MySessionFactory {
protected SessionFactory sessionFactory;
public AbstractSessionFactory() {
buildSessionFactory();
}
@Override
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Override
public void shutdownSessionFactory() {
if (sessionFactory != null) {
sessionFactory.close();
}
}
protected abstract SessionFactory buildSessionFactory();
}
4. 使用相应的类型
注释您的ORM工厂实施@ORMSessionFactory(ORMType.HIBERNATE)
public class HibernateSessionFactory extends AbstractSessionFactory {
@Override
protected SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry sr = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(sr);
return sessionFactory;
} catch (Exception ex) {
throw ex;
}
}
}
5. 然后,在RecordDAOImpl
您可以执行以下操作
public class RecordDAOImpl implements RecordDAO {
@Inject
@ORMSessionFactory(ORMType.HIBERNATE)
private MySessionFactory mySessionFactory;
@Override
public String getRecord(Long id) {
String value = null;
Session session = mySessionFactory.getSessionFactory().openSession();
try {
session.beginTransaction();
// do some transactional stuff
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
return value;
}
}
不要忘记,为了启用CDI,您应该在 META-INF (JAR)或 WEB-中包含 beans.xml INF 文件夹(WAR):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>