Hibernate DAO工厂无法实例化dao

时间:2011-10-06 20:59:56

标签: java hibernate factory dao

我有一个使用hibernate的JSF 2.0 / Servlet 3.0 Web应用程序,在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接口和实现似乎工作正常,但工厂无法实例化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();

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

下面,

return (ProfileDAO)instantiateDAO(ProfileDAO.class);

你正试图实例化一个抽象类。这和你要做的一样

profileDao = new ProfileDAO();

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

return (ProfileDAO)instantiateDAO(ProfileDAOHibernate.class);