gpt4 book ai didi

java - Spring Boot Hibernate 中的 H2 DB 不生成 Db Schema

转载 作者:搜寻专家 更新时间:2023-10-31 20:19:17 25 4
gpt4 key购买 nike

我希望我的 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]

实体管理器工厂

@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 我记不清了,我现在找不到代码。但我想是我需要返回 LocalEntityManagerFactory 而不是 EntityManagerFactory... 或类似的东西。祝你好运!也许我可以在接下来的几天找到代码...

最佳答案

创建数据源时,您需要设置 hbm2ddl.auto 属性,以便在启动时创建/更新数据库。

    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

关于java - Spring Boot Hibernate 中的 H2 DB 不生成 Db Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29758136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com