6

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");

and I've annotated my Entities like:

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

but the db schema is still not created.

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

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 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...

1
  • the url helped me, seems spring ignores the annotation on the entity. Commented May 8, 2020 at 19:35

1 Answer 1

3

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

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

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but for the copy-pasters among us: properties.put("hibernate.hbm2ddl.auto", "update");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.