Spring DB中的H2 DB Hibernate不会生成Db Schema [英] H2 DB in Spring Boot Hibernate does not generate Db Schema

查看:315
本文介绍了Spring DB中的H2 DB Hibernate不会生成Db Schema的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Spring应用程序自动生成数据库模式和表...
我已阅读了一些关于此主题的问答,并且已将我的数据库URL设置为:

I want my Spring Application to autogenerate the DB schema and tables... I've read some Q&A to this topic and I've set my DB URL to:


H2DataSource.setUrl(jdbc:h2:mem:tmp.db; INIT = CREATE SCHEMA if not
EXISTS GPSTRACKER);

H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT EXISTS GPSTRACKER");

我注释了我的实体,例如:

and I've annotated my Entities like:


@实体

@Table(name =tblGps,schema =GPSTRACKER)

@Entity
@Table(name="tblGps", schema= "GPSTRACKER")

但数据库模式仍然没有创建。

but the db schema is still not created.

这是我的日志输出。 Hibernate试图创建表,但无法找到模式!

Here is my log output. Hibernate is trying to create the tables, but can't find the schema!

我做错了什么?任何建议?

What am I doing wrong? Any suggestions?

记录输出

Log Output

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();
    }

DataSource $ b

DataSource

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 ...或者那样。祝你好运!也许我可以在接下来的几天找到代码...

@pvgoddijn i can't remember exactly, and i can't find the code right now. But I guess it was that I needed to return LocalEntityManagerFactory instead of EntityManagerFactory... or sth like that. good luck! maybe i can find the code the next days...

推荐答案

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

When creating your datasource, you need to set the hbm2ddl.auto property in order for the database to be created/updated on startup.

    Properties properties = new Properties();
    properties.put("hibernate.hbm2ddl.auto", "update");
    H2DataSource.setConnectionProperties(properties);

您也可以在您的hibernate.cfg.xml文件中设置属性

You could also set the property in your hibernate.cfg.xml file

其他可能的值是:validate |更新|创建| create-drop

Other possible values are: validate | update | create | create-drop

有关此属性和其他属性的更多信息,请参见: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html #配置 - 可选

Additional information about this and other properties can be found at: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional

这篇关于Spring DB中的H2 DB Hibernate不会生成Db Schema的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆