首先删除所有配置,Spring Boot会为你启动它。如果你确实需要a SessionFactory
而不是EntityManagerFactoryadd HibernateJpaSessionfactorybean
。
确保你application.properties
的类路径中有一个,然后添加以下属性。
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=latin1
spring.datasource.username=klebermo
spring.datasource.password=123
spring.jpa.database-platform=org.hibernate.dialect.PostgresqlDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create
如果你确实需要访问,SessionFactory
并且基本上是针对同一数据源的,则可以执行以下操作(尽管对于XML而不是JavaConfig,也可以在此处进行说明)。
@Configuration
public class HibernateConfig {
@Bean
public HibernateJpaSessionfactorybean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionfactorybean factory = new HibernateJpaSessionfactorybean();
factory.setEntityManagerFactory(emf);
return factory;
}
}
这样,你就可以同时拥有EntityManagerFactory
和SessionFactory
。
假设你有一个带有main方法的类,@EnableAutoConfiguration
则不需要@EnableTransactionManagement
注释,因为Spring Boot
将为你启用该注释。com.spring.app
包中的基本应用程序类就足够了。
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
这样的事情应该足以检测到你的所有类(包括实体和基于Spring Data的存储库)。
我还建议删除commons-dbcp
依赖关系,因为这将允许Spring Boot配置更快,更可靠的HikariCP实现。