PostgreSQL + Hibernate + Spring自动创建数据库 [英] PostgreSQL + Hibernate + Spring auto create database
问题描述
我正在使用PostgreSQL和Spring 4,并且希望我的应用在运行时自动创建数据库。
I'm working with PostgreSQL and Spring 4 and want my app auto create database when it running.
我的实体类是:
@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
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;
}
}
application.properties
application.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'不存在。
But when I running SQL to access table User, this will appear error: Table 'User' does not exist.
如何使Hibernate自动创建数据库?
How can I make Hibernate to auto create database?
推荐答案
属性 hibernate.hbm2ddl.auto
将为您解决问题。创建SessionFactory时,它将自动验证模式DDL或将其导出到数据库。使用create-drop可以在显式关闭SessionFactory时删除数据库架构。
The property hibernate.hbm2ddl.auto
will do the trick for you. It automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
Hibernate可以接受上述属性的这些选项。
Hibernate can accept these options for the above property.
validate
:验证架构,不对数据库进行任何更改。
validate
: validate the schema, makes no changes to the database.
update
:更新架构。
create
:创建模式,销毁先前的数据。
create
: creates the schema, destroying previous data.
创建-删除
:在会话结束时删除模式。
create-drop
: drop the schema at the end of the session.
这篇关于PostgreSQL + Hibernate + Spring自动创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!