hibernate4-maven-plugin

A simple Plugin for generating a Database-Schema from Hibernate 4 Mapping-Annotations

Hibernate comes with the buildin functionality, to automatically create or update the database schema. This functionality is configured in the session-configuraton via the parameter hbm2ddl.auto (see Hibernate Reference Documentation – Chapter 3.4. Optional configuration properties). But doing so is not very wise, because you can easily corrupt or erase your production database, if this configuration parameter slips through to your production environment.

Alternatively, you can run the tools SchemaExport or SchemaUpdate by hand. But that is not very comfortable and being used to maven you will quickly long for a plugin, that does that job automatically for you, when you fire up your test cases.

In the good old times, there was the Maven Hibernate3 Plugin, that does this for you. But unfortunatly, this plugin is not compatible with Hibernate 4.x. Since there does not seem to be any successor for the Maven Hibernate3 Plugin and googeling does not help, I decided to write up this simple plugin (inspired by these two articles I found: Schema Export with Hibernate 4 and Maven and Schema generation with Hibernate 4, JPA and Maven).

I hope, the resulting simple to use buletproof hibernate4-maven-plugin is usefull!

Try it out now!

30 thoughts on “hibernate4-maven-plugin”

  1. Hi thx for updating the plugin to support hibernate 5. :)

    Is there a way to generate “NOT NULL” constraints in the generated “output file” via existing @NotNull annotations (javax.validation.constraints.NotNull) like in the version before?
    I don’t like to add @column(nullable = false) to every field.

    Thx for your answer :)

  2. Hi.

    I tried to use version 1.0.3, but recieve exception “no annotated classes found in directory” (on MacOS El Capitan)

    I updated version to 1.1.0, but i have used a custom HibernateDialect class, that defined in my project and catch following exception


    Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.tdh.crm.util.JsonPostgreSQLDialect
    at de.juplo.plugins.hibernate4.ValidationConfiguration.secondPassCompile(ValidationConfiguration.java:46)

    I sure, that it is public class with noarg constructor, that extends PostgreSQL9Dialect.

    Can you help me?

    Best regards.

    1. Like always: starting over new was easier than understanding the existing code :/
      If I remember right, I took a quick view into the code of the hibernate3-maven-plugin and realised, that it can do a lot more, than simply generat the database schema.
      So, I decided to start over with a slim layer above the tools from hibernate.

    1. Multiple executions should work as expected, since the plugin obeys the standard rules for maven plugins.
      I also have seen working examples of this kind of configuration…

  3. Hi thanks a lot for making this plugin available, great work!!
    I have a problem generating postgres schema. Looks like the plugin ignores the data type when adding default values and that yields a syntax error from Postgres. Or maybe I’m doing something wrong. I’m using version 1.0.3.

    For example I have in java:

    @Column(name = “financialEnabled”, nullable = false, columnDefinition = “default TRUE”)
    private boolean financialEnabled;

    Generated SQL is:
    financialEnabled default TRUE not null,

    As you can see the data type boolean is not translated to the SQL script. Thanks a lot for your help.

    1. This plugin is only a tool to automate the generation of the SQL in your development-environment.
      Questions on how to anotate your code correctly are better asked in a user-forum from hibernate or such.

      Nevertheless, I think I can give you a usefull hint:
      You are overwriting the automatically generated column-definition with “default TRUE”.
      Try it with

      @Column(name = “financialEnabled”, nullable = false)
      private boolean financialEnabled;

  4. Hi,

    looks like a very nice plugin. Unfortunately, part of our entities are in other modules/dependencies.
    Do you plan to add the possibility to scan also for dependencies of the project or at least of the plugin?

    Also, when I only want to generate the SQL and use the following configuration, I got nothing.

    Config:

    de.juplo
    hibernate4-maven-plugin
    1.0.2

    true
    SCRIPT
    NONE
    com.deutscheboerse.hibernate.PostgreSQLDialect
    ${project.build.directory}/hibernate4/cmm-schema.sql

    com.deutscheboerse.energy
    energy-commons-hibernate
    ${commons.hibernate.version}

    org.springframework.security
    spring-security-core
    ${spring.security.version}

    org.slf4j
    slf4j-log4j12
    ${slf4j.version}

    Output:
    mvn hibernate4:export -e
    [INFO] Error stacktraces are turned on.
    [INFO] Scanning for projects…
    [INFO]
    [INFO] ————————————————————————
    [INFO] Building CMM WAR 1.0.0-RC5-SNAPSHOT
    [INFO] ————————————————————————
    [INFO]
    [INFO] — hibernate4-maven-plugin:1.0.2:export (default-cli) @ cmm-war —
    [INFO] Scanning directory D:\_dev\work\ii\src\cmm\trunk\cmm-war\target\classes f
    [INFO] No hibernate-properties-file found! (Checked path: D:\_dev\work\ii\src\cm
    [INFO] Gathered hibernate-configuration (turn on debugging for details):
    [INFO] hibernate.dialect = com.deutscheboerse.hibernate.PostgreSQLDialect
    [INFO] HHH000400: Using dialect: com.deutscheboerse.hibernate.PostgreSQLDialect
    [INFO] ————————————————————————
    [INFO] BUILD SUCCESS
    [INFO] ————————————————————————
    [INFO] Total time: 10.932s
    [INFO] Finished at: Thu Oct 10 12:51:05 UTC 2013
    [INFO] Final Memory: 9M/23M
    [INFO] ————————————————————————

    Thanks for any help,
    Milos.

    1. Yes, I am working on the possibility, to scan for annotations in dependencies.
      Unfortunatly, I have no example-project for this use-case by hand.
      It would help a lot, if you could provide a sample-project on github or such.

      Greetings kai

  5. Following my previous question, here is the debug info

    [DEBUG] Dependency: /Users/pmarques/.m2/repository/org/springframework/security/spring-security-acl/3.1.4.RELEASE/spring-security-acl-3.1.4.RELEASE.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/atomikos-util/3.6.5/atomikos-util-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-api/3.6.5/transactions-api-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/javax/transaction/transaction-api/1.1/transaction-api-1.1.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-jdbc-deprecated/3.6.5/transactions-jdbc-deprecated-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-jdbc/3.6.5/transactions-jdbc-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-jta/3.6.5/transactions-jta-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions/3.6.5/transactions-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/org/apache/geronimo/specs/geronimo-jta_1.0.1B_spec/1.0.1/geronimo-jta_1.0.1B_spec-1.0.1.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-jms-deprecated/3.6.5/transactions-jms-deprecated-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-jms/3.6.5/transactions-jms-3.6.5.jar
    [DEBUG] Dependency: /Users/pmarques/.m2/repository/com/atomikos/transactions-hibernate3/3.6.5/transactions-hibernate3-3.6.5.jar
    [INFO] Scanning directory /target/classes for annotated classes…

  6. Hi,

    I have the following problem.
    The project that I use to test (and use the plugin) has the annotated classes as a dependency.
    I am getting the error:
    No annotated classes found in directory /target/classes

    Shouldn’t the plugin scan all the dependencies also?

    Thanks,
    Pedro.

    1. Hi Pedro,

      I think, that your observation is right.
      But otherwise, dependencies should only be scanned if requested, because automatic scanning of the dependencies might lead to errors in other situations.

      If you can make your project available to me (for example via github, or simply by mailing zipped version), I would add a configuration-parameter to enable/disable dependency-scanning and upload the refined plugin to central.

      Regards,

      Kai Moritz

        1. Hi Kai,

          Unlike Pedro, I do not want my dependencies to be scanned. I tried passing an empty value for the “hibernate.export.scan_dependencies” parameter, but it still used the default value (compile).

          I tried passing something else like “system”, the behaviour did not change.

          How can I stop it from scanning my dependencies?

          Thanks,
          Tarek

          1. Passing -Dhibernate.export.scan_dependencies=none to the call to maven or setting <scanDependencies>none</scanDependencies> in the configuration of the hibernate4-maven-plugin should do the trick.

  7. Hello,

    I’m upgrading from hibernate3 to to hibernate4 and have moved from the hibernate3-maven-plugin to this version. I haven’t undertaken (and don’t want to just yet) the big job of changing my hbm mapping files to annotations.

    As far as I can see this is a show stopper for using your nice plugin. Can you please confirm if this is the case and whether you are planning to add support for scanning for hbm files?

    Many thanks,

    Mike Cohen.

    1. Hello Mike,

      I added the requested feature in the SNAPSHOT-version.
      It would be nice, if you could test, if the new feature works, because I have no example project by hand, that still uses hibernate-mapping via XML.

      You can download an actual build here:

      https://oss.sonatype.org/content/repositories/snapshots//de/juplo/hibernate4-maven-plugin/1.0.2-SNAPSHOT/

      or build it by yourself from the sources.
      The feature is documented here:

      http://juplo.de/hibernate4-maven-plugin-1.0.2-SNAPSHOT/export-mojo.html

      Best regards

      kai

  8. Hey I have modified your code to support envers and generate auditing tables, if you want I can send you a patch. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *