Database schema generation with Hibernate and Maven

Hi there,

I am trying to generate sql for my Vaadin project with Hibernate. So far, Eclipse has automatically updated the database, which gave me a good start to develop with Vaadin. But to have a healthy code lifecycle strategy, I want no automatic database updates but generated sql. I am trying to use
mvn hibernate3:hbm2ddl
but it gives errors.

Attached below:

  • maven error log
  • persistence.xml
  • pom.xml

Does anyone have an idea what the issue is? Or how do you generate your database changes?
Thanks for your help.

Andrea



Error:

[ERROR]
Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (default-cli) on project test2: There was an error creating the AntRun task. NullPointerException → [Help 1]

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (default-cli) on project test2: There was an error creating the AntRun task.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: There was an error creating the AntRun task.
at org.codehaus.mojo.hibernate3.AbstractHibernateMojo.execute(AbstractHibernateMojo.java:84)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
… 19 more
Caused by: java.lang.NullPointerException
at org.codehaus.plexus.configuration.DefaultPlexusConfiguration.add(DefaultPlexusConfiguration.java:175)
at org.codehaus.plexus.configuration.DefaultPlexusConfiguration.addChild(DefaultPlexusConfiguration.java:151)
at org.codehaus.mojo.hibernate3.util.PlexusConfigurationUtils.setHibernateConfiguration(PlexusConfigurationUtils.java:289)
at org.codehaus.mojo.hibernate3.util.PlexusConfigurationUtils.parseHibernateTool(PlexusConfigurationUtils.java:67)
at org.codehaus.mojo.hibernate3.AbstractHibernateToolMojo.getConfiguration(AbstractHibernateToolMojo.java:60)
at org.codehaus.mojo.hibernate3.AbstractHibernateMojo.execute(AbstractHibernateMojo.java:76)
… 21 more


persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="local">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:55641/test2" />
        <property name="hibernate.connection.username" value="..." />
        <property name="hibernate.connection.password" value="..." />
        <property name="hibernate.flushMode" value="FLUSH_AUTO" />          
        
  <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
  <property name="eclipselink.application-location" value="/sql" />
  <property name="eclipselink.create-ddl-jdbc-file-name" value="create.sql"/>
  <property name="eclipselink.drop-ddl-jdbc-file-name" value="drop.sql"/>
  <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
        </properties>
</persistence-unit>   


pom.xml (extract):

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>hibernate3-maven-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <hibernatetool>
                            <jpaconfiguration persistenceunit="local" />   
                            <hbm2ddl export="false" create="true"
                                update="true" format="true" outputfilename="${basedir}/.openshift/action_hooks/sql/resourcesschemaDiff.ddl" />             
                            </hibernatetool>
                    </configuration>
                </plugin>

I am using Vaadin 7.3.3 and a postgreSQL 9.3 database.

I forgot to mention that so far, the Eclipse JPA manager updated the database schema for me (
JPA Tools
- Generate Tables from Entities - Output Mode “Database”). However, the output mode “Sql-script” didn’t work and that’s why I tried to do it with Hibernate and Maven instead. It seemed to generate sql output but in the end the file didn’t exist:


Resource ‘/Swisscreate-20150203/createDDL.sql’ does not exist.

If you have an idea how to solve this issue, that would be great too!

Thanks,
Andrea

What you want to create is the sql schema of your database with hibernate?

Yes. I have the annotated model classes and want to generate the sql queries needed to create/update the database schema. I don’t mind if it’s done with the Eclipse JPA Tools or with maven and Hibernate but neither of them works at the moment.

And why do you want the sql file for creating the schema manually?

You can do it using postgresql, can’t you?

Do you mean why I don’t want it to be executed on the database automatically? I want to execute it manually because I need to execute it on the dev/test/production databases separately.

I think you can do it with your hibernate.cfg.xml: select the database manually.

Thanks for your suggestion. I think you are saying I should let Eclipse upgrade my production database as well, right? I am afraid that is no option for me. I have a dev, test and production server with separated databases and I work with database upgrade and downgrade scripts between all the software versions. Do you know what I mean?

I get it. You can’t let Hibernate delete your database or upgrade itself…