我在正确设置数据库方面遇到了问题。我观察到的行为是我想要的“hbm2ddl.auto = create-drop”或“ddl-auto = create-drop”。但是,我明确设置了“hbm2ddl.auto = validate”并删除了ddl-auto(并尝试了这两个的所有其他组合并验证),但仍然获得与“create-drop”相同的行为。
首先,我想要的是什么:
我的问题:
更新的(1): 修订版(2):
我的数据库是使用@Configuration配置的:
@Configuration
public class DatabaseConfig {
private final Environment env;
@Autowired
public DatabaseConfig(Environment env) {
this.env = env;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("datasource.driver"));
dataSource.setUrl(env.getProperty("datasource.url"));
dataSource.setUsername(env.getProperty("datasource.username"));
dataSource.setPassword(env.getProperty("datasource.password"));
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", env.getProperty("jpa.hibernate.dialect"));
hibernateProperties.put("hibernate.show_sql", env.getProperty("jpa.show-sql"));
hibernateProperties.put("spring.jpa.generate-ddl", env.getProperty("jpa.generate-ddl"));
sessionFactoryBean.setHibernateProperties(hibernateProperties);
return sessionFactoryBean;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
我使用.yaml作为hibernate属性: UPDATE:
spring.profiles: run_GA
datasource:
driver: org.h2.Driver
url: jdbc:h2:tcp://localhost/C:/Users/User/Document/MLDB;DB_CLOSE_DELAY=-1
username: sa
password:
jpa:
show-sql: false
generate-ddl: true
hibernate.dialect: org.hibernate.dialect.H2Dialect
entitymanager.packagesToScan: somePath.mapping
关于映射,我使用以下类:
@Entity
@Table
public class Machinedata {
public int timeInCycles;
public double sensor01;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "timeincycles")
public int getTimeInCycles() {
return timeInCycles;
}
public void setTimeInCycles(int timeInCycles) {
this.timeInCycles = timeInCycles;
}
@Column(name = "sensor01")
public double getSensor01() {
return sensor01;
}
public void setSensor01(double sensor01) {
this.sensor01 = sensor01;
}
当我在h2数据库中手动创建表时,我使用以下sql命令:
CREATE TABLE MACHINEDATA(Time IDENTITY AUTO_INCREMENT, sensor01 DOUBLE);
INSERT INTO Machinedata SELECT * FROM CSVREAD('myPath.csv');
import.sql文件显示以下命令:
INSERT INTO Machinedata (timeincycles, sensor01) SELECT * FROM CSVREAD('myPath.csv');
如果我设置hbm2ddl.auto = create-drop,我会期待所描述的行为。但是,正如我上面提到的,hbm2ddl.auto =在我的情况下验证。你有什么想法我的代码有什么问题吗?非常感谢您的帮助。
答案 0 :(得分:0)
尝试删除以下配置...
hibernate.ddl-auto: validate
hibernate.hbm2ddl.auto: validate
...而只是使用这个spring配置,你班上的任何更新或更改都只会更新当前。
spring.jpa.generate-ddl=true
答案 1 :(得分:0)
不确定它是否适用于您的代码,这对我有很大帮助。参见https://github.com/spring-projects/spring-boot/issues/1374
我已经设置:
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.hbm2ddl.auto=none
(缺少spring.jpa),此设置很受欢迎。 SpringBoot 2.1.3发布