Spring Boot Hibernate 中的 H2 DB 不生成 Db Schema
H2 DB in Spring Boot Hibernate does not generate Db Schema
我希望我的 Spring 应用程序自动生成数据库架构和表...
我已经阅读了有关该主题的一些问答,并将我的数据库 URL 设置为:
H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT
EXISTS GPSTRACKER");
并且我对我的实体进行了如下注释:
@Entity
@Table(name="tblGps", schema= "GPSTRACKER")
但数据库架构仍未创建。
这是我的日志输出。 Hibernate 正在尝试创建表,但找不到架构!
我做错了什么?有什么建议吗?
日志输出
2015-04-20 22:29:38.211 INFO 7056 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2015-04-20 22:29:38.356 INFO 7056 --- [ost-startStop-1] org.hibernate.Version : HHH000412: Hibernate Core {4.3.8.Final}
2015-04-20 22:29:38.360 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2015-04-20 22:29:38.362 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2015-04-20 22:29:38.745 INFO 7056 --- [ost-startStop-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-04-20 22:29:38.899 INFO 7056 --- [ost-startStop-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2015-04-20 22:29:39.202 INFO 7056 --- [ost-startStop-1] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2015-04-20 22:29:39.795 INFO 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop table GPSTRACKER.tbl_gps if exists
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : Schema "GPSTRACKER" nicht gefunden
Schema "GPSTRACKER" not found; SQL statement:
drop table GPSTRACKER.tbl_gps if exists [90079-185]
EntityManagerFactory
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
vendorAdapter.setDatabasePlatform(MyAppSettings.getDbPlattform());
HibernateJpaDialect jpd = new HibernateJpaDialect();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaDialect(jpd);
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(MyAppSettings.packagesToScan);
factory.setDataSource(MyDataSource());
return factory.getObject();
}
数据源
DriverManagerDataSource H2DataSource = new DriverManagerDataSource();
H2DataSource.setDriverClassName("org.h2.Driver");
H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT EXISTS GPSTRACKER");
H2DataSource.setUsername("sa");
H2DataSource.setPassword("");
@pvgoddijn 我记不清了,我现在也找不到代码。但我想是我需要 return LocalEntityManagerFactory
而不是 EntityManagerFactory
... 或类似的东西。祝你好运!也许我可以在接下来的几天找到代码...
创建数据源时,您需要设置 hbm2ddl.auto 属性 以便数据库在启动时为 created/updated。
Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto", "update");
H2DataSource.setConnectionProperties(properties);
您还可以在 hibernate.cfg.xml 文件中设置 属性
其他可能的值是:验证 |更新 |创建 |创建下降
有关此属性和其他属性的其他信息,请访问:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional
我希望我的 Spring 应用程序自动生成数据库架构和表... 我已经阅读了有关该主题的一些问答,并将我的数据库 URL 设置为:
H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT EXISTS GPSTRACKER");
并且我对我的实体进行了如下注释:
@Entity
@Table(name="tblGps", schema= "GPSTRACKER")
但数据库架构仍未创建。
这是我的日志输出。 Hibernate 正在尝试创建表,但找不到架构!
我做错了什么?有什么建议吗?
日志输出
2015-04-20 22:29:38.211 INFO 7056 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2015-04-20 22:29:38.356 INFO 7056 --- [ost-startStop-1] org.hibernate.Version : HHH000412: Hibernate Core {4.3.8.Final}
2015-04-20 22:29:38.360 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2015-04-20 22:29:38.362 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2015-04-20 22:29:38.745 INFO 7056 --- [ost-startStop-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-04-20 22:29:38.899 INFO 7056 --- [ost-startStop-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2015-04-20 22:29:39.202 INFO 7056 --- [ost-startStop-1] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2015-04-20 22:29:39.795 INFO 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop table GPSTRACKER.tbl_gps if exists
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : Schema "GPSTRACKER" nicht gefunden
Schema "GPSTRACKER" not found; SQL statement:
drop table GPSTRACKER.tbl_gps if exists [90079-185]
EntityManagerFactory
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
vendorAdapter.setDatabasePlatform(MyAppSettings.getDbPlattform());
HibernateJpaDialect jpd = new HibernateJpaDialect();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaDialect(jpd);
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(MyAppSettings.packagesToScan);
factory.setDataSource(MyDataSource());
return factory.getObject();
}
数据源
DriverManagerDataSource H2DataSource = new DriverManagerDataSource();
H2DataSource.setDriverClassName("org.h2.Driver");
H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT EXISTS GPSTRACKER");
H2DataSource.setUsername("sa");
H2DataSource.setPassword("");
@pvgoddijn 我记不清了,我现在也找不到代码。但我想是我需要 return LocalEntityManagerFactory
而不是 EntityManagerFactory
... 或类似的东西。祝你好运!也许我可以在接下来的几天找到代码...
创建数据源时,您需要设置 hbm2ddl.auto 属性 以便数据库在启动时为 created/updated。
Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto", "update");
H2DataSource.setConnectionProperties(properties);
您还可以在 hibernate.cfg.xml 文件中设置 属性
其他可能的值是:验证 |更新 |创建 |创建下降
有关此属性和其他属性的其他信息,请访问:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional