【发布时间】:2018-10-25 18:54:00
【问题描述】:
我有一个 Spring Boot Web 应用程序,它尝试使用 Hibernate 访问我的数据库。 我还有一个不同的、没有尝试访问同一个数据库的 spring boot 应用程序。
我正在尝试使用自动配置方法配置 Spring Boot 应用程序,在 application.properties 文件中定义我的数据库属性。
我正在使用带注释的类配置无 spring boot 应用程序。
出于某种原因,Spring Boots 自动配置与带注释的类配置的行为不同。
如果我第一次连接到数据库并使用 hibernate ddl 创建架构,然后我使用其他配置方式重新连接,则会出现 ddl 错误,例如枚举列停止工作。
有人可以解释为什么我使用两种配置 spring 和 hibernate 的方式会出现不同的行为,以及如何让它们以相同的方式运行?这可能与 ejb 命名策略属性有关吗?
这是我配置 JPA 的两种方式:
application.properties:
spring.datasource.url=jdbc:hsqldb:file:databaseFiles/hibData/;hsqldb.write_delay_millis=0
spring.datasource.root=sa
spring.datasource.password=1
spring.datasource.driverClassName=org.hsqldb.jdbcDriver
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
配置类:
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
public class DatabaseHibConfig {
@Bean
public DataSource getDataSource(){
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:file:databaseFiles/hibData/;hsqldb.write_delay_millis=0");
ds.setUsername("sa");
ds.setPassword("1");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory, DataSource dataSource){
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setDataSource(dataSource);
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
return jpaTransactionManager;
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("mediabeast.data.hibernate.model");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
jpaProperties.put("hibernate.hbm2ddl.auto","update");
jpaProperties.put("hibernate.show_sql","true");
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
}
【问题讨论】:
标签: spring hibernate spring-boot jpa spring-data-jpa