开发者

Hibernate DAO factory cannot instantiate dao

I have a JSF 2.0/Servlet 3.0 Web application using hibernate with a connection pooled data source on glassfish 3. I tried using the DAO pattern as described in this tutorial:

GenericDataAccessObjects I am also using the OpenSessionView pattern using the HibernateSessionRequestFilter as described here: OpenSessionInView The relevant parts of my hibernate config:

<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>

The DAO interfaces and implementations seem to work fine but the factory is not able to instantiate the DAOs. If I instantiate them manually in my ManagedBean:

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

But using the factory:

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

fails with 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)

the end:

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)

The 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();
}

The DAOFactory:

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

/**
 * Factory method 开发者_Python百科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();

Any help is appreciated.


Here,

return (ProfileDAO)instantiateDAO(ProfileDAO.class);

you're trying to instantiate an abstract class. This is the same as you would be doing

profileDao = new ProfileDAO();

This is not possible. You need to instantiate a concrete class.

return (ProfileDAO)instantiateDAO(ProfileDAOHibernate.class);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜