java - Hibernate DAO工厂无法实例化dao

标签 java hibernate factory dao

我有一个 JSF 2.0/Servlet 3.0 Web 应用程序,使用 hibernate 和 glassfish 3 上的连接池数据源。 我尝试使用本教程中描述的 DAO 模式:
GenericDataAccessObjects 我还使用使用 HibernateSessionRequestFilter 的 OpenSessionView 模式,如下所述:OpenSessionInView 我的 hibernate 配置的相关部分:

<property name="hibernate.current_session_context_class">jta</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</property>

DAO 接口(interface)和实现似乎工作正常,但工厂无法实例化 DAO。如果我在 ManagedBean 中手动实例化它们:

profileDao = new ProfileDAOHibernate();
profileDao.setSession(HibernateUtil.getSessionFactory().getCurrentSession());

但是使用工厂:

factory = DAOFactory.instance(DAOFactory.HIBERNATE);
profileDao = factory.getProfileDAO();

因 RuntimeException 失败:

SCHWERWIEGEND: java.lang.InstantiationException: model.dao.interfaces.ProfileDAO
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at model.dao.factory.hibernate.HibernateDAOFactory.instantiateDAO(HibernateDAOFactory.java:87)
at model.dao.factory.hibernate.HibernateDAOFactory.getProfileDAO(HibernateDAOFactory.java:62)
at controller.admin.ProfileController.<init>(ProfileController.java:73)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)

结束:

SCHWERWIEGEND: ava:357)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at appHelper.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

HibernateDAOFactory:

// getters for the DAO
@Override
public ProfileDAO getProfileDAO() {
    return (ProfileDAO)instantiateDAO(ProfileDAO.class);
}
//...
private GenericHibernateDAO instantiateDAO(Class daoClass) {
    try {
        GenericHibernateDAO dao = (GenericHibernateDAO)daoClass.newInstance();
        dao.setSession(getCurrentSession());

        return dao;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Can not instantiate DAO: " + daoClass, ex);
    }
}

// You could override this if you don't want HibernateUtil for lookup
protected Session getCurrentSession() {
    return HibernateUtil.getSessionFactory().getCurrentSession();
}

DAOFactory:

public static final Class HIBERNATE = model.dao.factory.hibernate.HibernateDAOFactory.class;

/**
 * Factory method for instantiation of concrete factories.
 */
public static DAOFactory instance(Class factory) {
    try {
        return (DAOFactory)factory.newInstance();
    } catch (Exception ex) {
        throw new RuntimeException("Couldn't create DAOFactory: " + factory);
    }
}

// DAO Interfaces
public abstract ProfileDAO getProfileDAO();

感谢任何帮助。

最佳答案

这里,

return (ProfileDAO)instantiateDAO(ProfileDAO.class);

您正在尝试实例化一个抽象类。这与您要做的相同

profileDao = new ProfileDAO();

这是不可能的。您需要实例化一个具体的类。

return (ProfileDAO)instantiateDAO(ProfileDAOHibernate.class);

关于java - Hibernate DAO工厂无法实例化dao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7680398/

相关文章:

java - 在 JRE 1.8.0_51 运行时安装 CA 根证书

java - Hibernate 5 给出错误 : must have same number of columns as the referenced primary key

javascript - 从服务器获取数据并使用 $http 服务和 ng-change 在选择事件上显示它

java - JSP中多部分请求时获取参数

需要 Java websocket 客户端

java - 错误 :(23, 17) 无法解析 : junit:junit:4. 12

java - NoSQL 数据存储提供程序 : org. hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider

hibernate - Spring4 @Scheduled @Transaction 抛出多个数据源刷新时没有事务正在进行

angularjs - 使用 angular.factory 时更新 socket.io 握手数据

javascript - Angularjs Factory 延迟数据消失