gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:09:23 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com