gpt4 book ai didi

java - 调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException : 'sessionFactory' or 'hibernateTemplate' is required

转载 作者:行者123 更新时间:2023-11-30 02:31:05 32 4
gpt4 key购买 nike

当我运行以下代码时,出现错误“调用 init 方法失败;嵌套异常为 java.lang.IllegalArgumentException:需要 'sessionFactory' 或 'hibernateTemplate'” 我的代码中的错误是什么?

EmployeeDaoImpl.java

@Repository
public class EmployeeDaoImpl extends AbstractHibernateDao implements EmployeeDao{
public Employee getSalaryDetails(Integer empId)
{
Session session = getSession();
Query query = session.getNamedQuery("getEmpSalaryDtls");
query.setInteger("empId", empId);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
Map map = (Map) query.uniqueResult();
closeSession(session);
return map;
}

抽象HibernateDao.java

public abstract class AbstractHibernateDao extends HibernateDaoSupport {
public Session getSession() {
HibernateTemplate template = this.getHibernateTemplate();
template.setSessionFactory(getSessionFactory());
return template.getSessionFactory().openSession();
}

错误消息是:

"192.24.0.77" "sourcePrg" 2017-05-30 04:41:14 "org.springframework.web.struts.ContextLoaderPlugIn" "Context initialization failed"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EmployeeDaoImpl' defined in file [D:\....\EmployeeDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.createWebApplicationContext(ContextLoaderPlugIn.java:355) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.initWebApplicationContext(ContextLoaderPlugIn.java:296) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.init(ContextLoaderPlugIn.java:226) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:1158) ~[struts-1.1.jar:1.1]
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:473) ~[struts-1.1.jar:1.1]
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:225) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:82) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2488) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:98) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$UpOneLevel.run(CurrentTaskFuture.java:753) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263) ~[weblogic.server.merged.jar:12.1.3.0.0]

Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate5.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]

现在我得到了这个错误:

"org.springframework.web.struts.ContextLoaderPlugIn" "Context initialization failed"
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'EmployeeServiceImpl': Unsatisfied dependency expressed through field 'EmployeeDao'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EmployeeDao' defined in ServletContext resource [/WEB-INF/spring-beans.xml]: Error setting property values; nested exception is
org.springframework.beans.NotWritablePropertyException: Invalid property 'hibernateTemplate' of bean class [com.temp.EmployeeDaoImpl]: Bean property 'hibernateTemplate' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?

最佳答案

使用HibernateTemplate自 hibernate 3.0.1 引入对上下文 session 的支持(大约在 2008 年!)以来,它就不再被鼓励了。现在,我们鼓励/建议您编写一个针对 the plain Hibernate API 的 DAO。 .

话虽如此,将您的 DAO 重写为以下内容。

public abstract class AbstractHibernateDao {

@Autowired
private SessionFactory sessionFactory;

protected Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}

现在您实际实现的 DAO

@Repository
public class EmployeeDaoImpl extends AbstractHibernateDao implements EmployeeDao{
public Employee getSalaryDetails(Integer empId)
{
Session session = getCurrentSession();
Query query = session.getNamedQuery("getEmpSalaryDtls");
query.setInteger("empId", empId);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
Map map = (Map) query.uniqueResult();
return map;
}

这里做了一些假设。

  1. 您已通过 HibernateTransactionManager 进行了正确的交易设置
  2. 您有一笔来自 @Transactional 的服务的实际交易
  3. 您已启用 @EnableTransactionManagement 的交易或<tx:annotation-driven />

今年 Spring 负责打开/关闭 Session对于你来说,你不必再考虑它了。

关于java - 调用init方法失败;嵌套异常是 java.lang.IllegalArgumentException : 'sessionFactory' or 'hibernateTemplate' is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44258160/

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