我正在使用 PostgreSQL 和 Spring 4,并希望我的应用程序在运行时自动创建数据库。
我的实体类是:
@Entity
@Table(name = "user", schema = "public")
public class User extends BaseEntity {
private Integer id;
private String name;
private Integer contractId;
public User() {
}
public User(Integer id) {
super(id);
}
@Id
@Column(name = "usr_id", nullable = false)
@GeneratedValue(strategy= GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "usr_name", nullable = true, length = -1)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "usr_contract_id", nullable = true)
public Integer getContractId() {
return contractId;
}
public void setContractId(Integer contractId) {
this.contractId = contractId;
}
}
HibernateConfig.java
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@PropertySources({
@PropertySource(value = "classpath:application.properties")})
@ConfigurationProperties(prefix = "spring.datasource")
public class HibernateConfig {
@Autowired
private Environment environment;
@Autowired
private DataSource dataSource;
@Autowired
private MultiTenantConnectionProvider multiTenantConnectionProvider;
@Autowired
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
public HibernateConfig() {}
@Bean
public LocalSessionFactoryBean sessionFactory() throws Exception {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setHibernateProperties(hibernateProperties());
sessionFactory.setPackagesToScan(new String[] {
"com.xxx.xxx.model",
});
return sessionFactory;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put(DIALECT, environment.getRequiredProperty(DIALECT));
properties.put(SHOW_SQL, environment.getRequiredProperty(SHOW_SQL));
properties.put(FORMAT_SQL, environment.getRequiredProperty(FORMAT_SQL));
properties.put(HBM2DDL_AUTO, environment.getRequiredProperty(HBM2DDL_AUTO));
return properties;
}
@Bean
@Primary
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
@Bean
@Autowired
public HibernateTemplate hibernateTemplate(SessionFactory s) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(s);
return hibernateTemplate;
}
}
应用程序.properties
# Database connection settings:
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/database
jdbc.username=postgres
jdbc.password=111111
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.hbm2ddl.auto=update
spring.datasource.initialSize=50
spring.datasource.maxActive=200
spring.datasource.maxIdle=200
spring.datasource.minIdle=50
但是当我运行 SQL 访问表 User 时,会出现错误:表 ‘User’ 不存在。
如何让 Hibernate 自动创建数据库?
原文由 Tran Vinh Quang 发布,翻译遵循 CC BY-SA 4.0 许可协议
该属性
hibernate.hbm2ddl.auto
将为您解决问题。它会在创建 SessionFactory 时自动验证或将模式 DDL 导出到数据库。使用 create-drop,当显式关闭 SessionFactory 时将删除数据库模式。Hibernate 可以接受上述属性的这些选项。
validate
:验证架构,不对数据库进行任何更改。update
:更新架构。create
:创建模式,破坏以前的数据。create-drop
:在会话结束时删除架构。