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!

15 Responses to “hibernate4-maven-plugin”

  1. Jukes says:

    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.

    • Kai Moritz says:

      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;

  2. Milios says:

    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.

  3. Pedro says:

    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…

  4. Pedro says:

    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.

    • Kai Moritz says:

      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

  5. mike says:

    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.

  6. Victor says:

    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

Logged in as Kai Moritz. Log out »