Java 如何修复';sessionFactory';或';hibernateTemplate';这是一个必须解决的问题
我正在使用SpringBootJPA、web和MYSQL创建我的web应用程序。它总是说“Java 如何修复';sessionFactory';或';hibernateTemplate';这是一个必须解决的问题,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,我正在使用SpringBootJPA、web和MYSQL创建我的web应用程序。它总是说“sessionFactory或hibernateTemplate是必需的”。我怎样才能修好它 我已经试过了: 已删除本地maven repo中hibernate core所在的路径 在application.properties中放入spring.jpa.properties.hibernate.current\u session\u context\u class=org.springframework.
sessionFactory
或hibernateTemplate
是必需的”。我怎样才能修好它
我已经试过了:
spring.jpa.properties.hibernate.current\u session\u context\u class=org.springframework.orm.hibernate5.SpringSessionContext
@自动连线
私人会话工厂会话工厂代码>在HibernateDaoSupport
扩展中
EntityManageConfig
和SessionConfig
文件@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManageFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.coolspen.rjb.dao"}
)
public class EntityManageConfig {
@Autowired
@Qualifier("myDataSource")
private DataSource myDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder){
return entityManageFactory(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManageFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManageFactory(EntityManagerFactoryBuilder builder){
LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(myDataSource)
.packages("com.coolspen.rjb.model").build();
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
jpaProperties.put("hibernate.connection.charSet", "utf-8");
jpaProperties.put("hibernate.show_sql", "false");
jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManageFactory(builder).getObject());
}
}
@Configuration
public class SessionConfig {
@Autowired
@Qualifier("myDataSource")
private DataSource myDataSource;
@Bean
public HibernateTransactionManager getTransationManager() {
return new HibernateTransactionManager(getSessionFactory());
}
@Bean
public SessionFactory getSessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(myDataSource);
builder.scanPackages("com.coolspen.rjb.dao");
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
builder.addProperties(hibernateProperties);
return builder.buildSessionFactory();
}
}
>Error starting ApplicationContext. To display the conditions report re-run >your application with 'debug' enabled.
2018-12-27 19:05:58.020 ERROR 18860 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'departmentDao' defined in file [E:\eclipse_project\rjb-13\target\classes\com\coolspen\rjb\dao\DepartmentDao.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:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.coolspen.rjb.Rjb13Application.main(Rjb13Application.java:21) [classes/:na]
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate5.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:122) ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 16 common frames omitted
SessionConfig.java:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManageFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.coolspen.rjb.dao"}
)
public class EntityManageConfig {
@Autowired
@Qualifier("myDataSource")
private DataSource myDataSource;
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder){
return entityManageFactory(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManageFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManageFactory(EntityManagerFactoryBuilder builder){
LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(myDataSource)
.packages("com.coolspen.rjb.model").build();
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
jpaProperties.put("hibernate.connection.charSet", "utf-8");
jpaProperties.put("hibernate.show_sql", "false");
jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
entityManagerFactory.setJpaProperties(jpaProperties);
return entityManagerFactory;
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManageFactory(builder).getObject());
}
}
@Configuration
public class SessionConfig {
@Autowired
@Qualifier("myDataSource")
private DataSource myDataSource;
@Bean
public HibernateTransactionManager getTransationManager() {
return new HibernateTransactionManager(getSessionFactory());
}
@Bean
public SessionFactory getSessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(myDataSource);
builder.scanPackages("com.coolspen.rjb.dao");
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
builder.addProperties(hibernateProperties);
return builder.buildSessionFactory();
}
}
>Error starting ApplicationContext. To display the conditions report re-run >your application with 'debug' enabled.
2018-12-27 19:05:58.020 ERROR 18860 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'departmentDao' defined in file [E:\eclipse_project\rjb-13\target\classes\com\coolspen\rjb\dao\DepartmentDao.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:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
at com.coolspen.rjb.Rjb13Application.main(Rjb13Application.java:21) [classes/:na]
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate5.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:122) ~[spring-orm-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 16 common frames omitted
DepartmentDao.java
@SuppressWarnings(value = "all")
@Repository
public class DepartmentDao extends BaseHibernateDao<Department,java.lang.Integer>{
public Class getEntityClass() {
return Department.class;
}
public Page<Department> findPage(DepartmentQuery query) {
StringBuilder hqlSb = new StringBuilder("SELECT t FROM Department t WHERE 1=1 ");
if(isNotEmpty(query.getDepartmentid())) {
hqlSb.append(" AND t.departmentid = :departmentid ");
}
if(isNotEmpty(query.getDepartmentName())) {
hqlSb.append(" AND t.departmentName = :departmentName ");
}
if(isNotEmpty(query.getRemark())) {
hqlSb.append(" AND t.remark = :remark ");
}
if(isNotEmpty(query.getCreatedateBegin())) {
hqlSb.append(" AND t.createdate >= :createdateBegin ");
}
if(isNotEmpty(query.getCreatedateEnd())) {
hqlSb.append(" AND t.createdate <= :createdateEnd ");
}
if(isNotEmpty(query.getCreator())) {
hqlSb.append(" AND t.creator = :creator ");
}
if(isNotEmpty(query.getModifier())) {
hqlSb.append(" AND t.modifier = :modifier ");
}
if(isNotEmpty(query.getModifydateBegin())) {
hqlSb.append(" AND t.modifydate >= :modifydateBegin ");
}
if(isNotEmpty(query.getModifydateEnd())) {
hqlSb.append(" AND t.modifydate <= :modifydateEnd ");
}
if(isNotEmpty(query.getIsAvaliable())) {
hqlSb.append(" AND t.isAvaliable = :isAvaliable ");
}
if(isNotEmpty(query.getIsDeleted())) {
hqlSb.append(" AND t.isDeleted = :isDeleted ");
}
System.out.println("finished1");
return pageQuery(hqlSb.toString(),query);
}
}
@SuppressWarnings(value = "all")
public abstract class BaseHibernateDao<E, PK extends Serializable>
extends
HibernateDaoSupport implements EntityDao<E, PK> {
/**
* Logger for subclass
*/
protected Log log = LogFactory.getLog(getClass());
public abstract Class getEntityClass();
@Autowired
private SessionFactory sessionFactory;
......
}
异常是由于
HibernateTemplate
内部HibernateDaoSupport
为空。您必须调用HibernateDaoSupport#setSessionFactory(sessionFactory)
来初始化HibernateTemplate
使用构造函数注入sessionFactory,然后调用此setter初始化HibernateTemplate
:
public abstract class BaseHibernateDao<E, PK extends Serializable>
extends HibernateDaoSupport implements EntityDao<E, PK> {
@Autowired
public BaseHibernateDao(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
}
基于HibernateDao的公共抽象类
扩展HibernateDaoSupport实现EntityDao{
@自动连线
基于公共的HibernateDAO(SessionFactory SessionFactory){
超级会话工厂(会话工厂);
}
}
以及您的实际DAO类:
@Repository
public class DepartmentDao extends BaseHibernateDao<Department,java.lang.Integer>{
public DepartmentDao(SessionFactory sessionFactory) {
super(sessionFactory);
}
}
@存储库
公共类DepartmentDao扩展BaseHibernateDao{
公共部门DAO(SessionFactory SessionFactory){
超级(sessionFactory);
}
}
你能发布部门DAO的代码片段吗?我已经发布了。如何在BaseHibernateDao中提供SessionFactory
?Springbean在简单的Java类中不可用,我认为您需要使用一些注释来注释BaseHibernateDao
,或者您必须做一些其他的事情,这样您的dao将能够访问SessionFactory
我只是将这个项目从SSH传输到Spring boot,在此之前,我只是在xml文件中配置。我刚刚更新了上面的内容。非常感谢非常感谢非常感谢,在尝试了您的建议后,我收到了很多消息。因此,您在本文中提到的消息sessionFactory或hibernateTemplate是必需的
在尝试了我的建议后已消失并得到解决?这个错误的信息是另一个吗?我刚刚意识到这是另一个问题