Spring Boot Hibernate自动配置与手动注释配置
我有一个SpringBootWeb应用程序,它试图使用Hibernate访问我的数据库。 我还有一个不同的非spring启动应用程序,它尝试访问同一个数据库 我正在尝试使用自动配置方法配置spring引导应用程序,在application.properties文件中定义我的DB属性 我正在使用注释类配置none spring启动应用程序 出于某种原因,Spring Boots自动配置的行为与注释类配置不同 如果我第一次连接到数据库并使用hibernate ddl创建模式,然后使用另一种配置方式重新连接,则会出现ddl错误,例如枚举列停止工作 有人能解释一下为什么我使用配置spring和hibernate的两种方法得到不同的行为,以及我如何让它们以相同的方式工作?这可能与ejb命名策略属性有关吗 以下是我配置JPA的两种方式: application.properties:Spring Boot Hibernate自动配置与手动注释配置,spring,hibernate,spring-boot,jpa,spring-data-jpa,Spring,Hibernate,Spring Boot,Jpa,Spring Data Jpa,我有一个SpringBootWeb应用程序,它试图使用Hibernate访问我的数据库。 我还有一个不同的非spring启动应用程序,它尝试访问同一个数据库 我正在尝试使用自动配置方法配置spring引导应用程序,在application.properties文件中定义我的DB属性 我正在使用注释类配置none spring启动应用程序 出于某种原因,Spring Boots自动配置的行为与注释类配置不同 如果我第一次连接到数据库并使用hibernate ddl创建模式,然后使用另一种配置方式重
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.jpa.properties.hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.implicit_naming_strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
但是,在切换配置时,启动时仍会出现“对象已存在”ddl错误