Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate schema in dropwizard-hibernate

I followed the tutorial for dropwizard and hibernate without problems. Now I have non trivial annotations in my entities, and I would like hibernate to generate the tables for me, and stuff like that.So, how can I change hibernate's configuration? Can I give it a hibernate.cfg.xml? If I can, do I have to set up the connection again?

I found this PR, but it doesn't seem to be in the public release yet (no hibernateBundle.configure in my jars)

But maybe I'm looking for the wrong thing. So far, I'm just trying to set hibernate.hbm2dll.auto. After all, there might be an other way to enable hibernate table generation in Dropwizard... So, any help?

Thank you.


Edit: I approached the problem from another angle, to explicitly create the schema instead of using hbm2ddl.auto. See proposed answer.

like image 357
dgn Avatar asked Jun 09 '13 23:06

dgn


People also ask

What is Dropwizard hibernate?

The dropwizard-hibernate module provides you with managed access to Hibernate, a powerful, industry-standard object-relation mapper (ORM).

What is schema in hibernate?

Hibernate provides a tool to automatically generate the database schema from the mapping files. The generated schema includes 'create table', 'alter table', referential integrity constraints, primary and foreign keys. Create some POJO persistent classes. Create mapping files. Configure the Dialect.


1 Answers

Edit: Problem solved! Doing this in the YAML config currently works: (Dropwizard 0.7.1)

database:
    properties:
        hibernate.dialect: org.hibernate.dialect.MySQLDialect
        hibernate.hbm2ddl.auto: create

(from this answer)


Old answer:

This is what I am currently using: A class that calls hibernate's SchemaExport to export the schema to a SQL file or to modify the database. I just run it after changing my entities, and before running the application.

public class HibernateSchemaGenerator {

    public static void main(String[] args) {
        Configuration config = new Configuration();

        Properties properties = new Properties();

        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/db"); 
        properties.put("hibernate.connection.username", "user");
        properties.put("hibernate.connection.password", "password");
        properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        properties.put("hibernate.show_sql", "true");
        config.setProperties(properties);

        config.addAnnotatedClass(MyClass.class);

        SchemaExport schemaExport = new SchemaExport(config);

        schemaExport.setOutputFile("schema.sql");
        schemaExport.create(true, true);

    }

}

I didn't know about hibernate tools before. So this code example can be used in the service initialization to act like hbm2ddl.auto = create.

I'm currently using it just by running the class (from eclipse or maven) to generate and review the output SQL.

like image 177
dgn Avatar answered Oct 06 '22 23:10

dgn