2

I have updated from Hibernate 4.3 to the latest version, currently 5.2.10.Final.

I have the need for maven to generate the ddl schema so that when I run a drop database then create schema it will pick up the generated schmea which I am trying to get an ant task in maven to run. In my previous version I used...

<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>

I have followed the instructions found here so within my pom.xml I now have the following...

<plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>generate-ddl-create</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <target name="schemaexport" depends="build-demo" description="Exports a generated schema to DB and file">
                              <taskdef
                                name="hibernatetool"
                                classname="org.hibernate.tool.ant.HibernateToolTask"
                                classpathref="maven.dependency.classpath"                                   
                              />
                              <hibernatetool destdir=".">
                                <classpath>             
                                  <path location="src/main/java"/>
                                </classpath>
                                <jpaconfiguration persistenceunit="randb_MariaDb" />
                                <hbm2ddl console="false" export="false" update="false" drop="false" create="true" outputfilename="/schema_MariaDb.sql" format="true" haltonerror="true"/>
                              </hibernatetool>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>            
                </execution>
            </executions>
        </plugin>  

My persistence.xml looks like this...

<persistence-unit name="randb_MariaDb" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <!-- Comment this our if you want to use properties below -->
    <non-jta-data-source>java:/comp/env/jdbc/randb_MariaDb</non-jta-data-source>
    <properties>
        <!-- 
        <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/ran" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="r00tPwd" />
        <property name="hibernate.connection.shutdown" value="true" />
        <property name="hibernate.connection.pool_size" value="0"/>
        <property name="hibernate.connection.aggressive_release" value="true"/>
         -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.jdbc.batch_size" value="20"/>
    </properties>
</persistence-unit>

My problem is that when I run mvn antrun:run@generate-ddl-create I get the following exception...

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-       plugin:1.7:run (generate-ddl-create) on project WimacRanServer: An Ant BuildException has occured: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/comp/env/jdbc/randb_MariaDb]
[ERROR] around Ant part ...<hibernatetool destdir=".">... @ 5:30 in /Users/dave/gitRepository/WimacRanServer/target/antrun/build-main.xml: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (generate-ddl-create) on project WimacRanServer: An Ant BuildException has occured: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/comp/env/jdbc/randb_MariaDb]
around Ant part ...<hibernatetool destdir=".">... @ 5:30 in /Users/dave/gitRepository/WimacRanServer/target/antrun/build-main.xml
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
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:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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)

I am guessing it may be something to do with the non-jta-data-source and have tried a few things like removing that and uncommenting the rest of the properties but to no avail. If anyone has come across a similar problem or can shed some light it would be greatly received.

  • with hibernate 5 you don't need the persistence.xml at all – XtremeBaumer Jun 01 '17 at 11:31
  • I did read in the docs that you don't have to provide a persistence.xml but from my understanding you still can. The example which is failing above previously worked using the mvn hibernate3:hbm2ddl before I upgraded hibernate and I am trying to get back to a point where I can use a similar command like the one I mentioned to generate the schema within a mavn build. – David Rowntree Jun 01 '17 at 12:24
  • i myself never used ANT, but what i know is that hibernate will generate a table in the specified database if it does not exist. for hibernate4 program i have a persistence.xml as well as an application.xml for spring in which i define the database connection – XtremeBaumer Jun 01 '17 at 12:28
  • Try this tool: https://github.com/Devskiller/hbm2ddl-maven-plugin This plugins supports Hibernate 5.2+ and more – MariuszS Oct 19 '17 at 07:40
  • @MariuszS I have just got around to putting this plugin to my project, it's exactly what I need. I have one little problem and I am hoping it may just be a config. When I generate my schema from the model classes it also creates the sequence tables which causes problems in my build. As an example I see create table ZcooResultSeq (next_val bigint) engine=InnoDB; insert into ZcooResultSeq values ( 1 ); because of @SequenceGenerator(name="ZcooResultId", sequenceName="ZcooResultSeq", initialValue=1, allocationSize=50) in my class, basically I don't want to see them I just want the create tables. – David Rowntree Nov 02 '17 at 20:38
  • @DavidRowntree main developer said that `hibernate.id.new_generator_mappings=false` should help :) – MariuszS Nov 03 '17 at 08:23
  • 1
    @DavidRowntree it looks like you have to implement your SchemaFilter and use it by `hibernate.hbm2ddl.schema_filter_provider` property set to the qualified class name. Sample provider is here: https://gist.github.com/jkubrynski/f5b251b6bfe162afc6fafec8489dcd1a – Jakub Kubrynski Nov 03 '17 at 08:33

1 Answers1

3

Try https://github.com/Devskiller/jpa2ddl, this tool supports latest hibernate version.

Sample usage:

<plugin>
    <groupId>com.devskiller.jpa2ddl</groupId>
    <artifactId>jpa2ddl-maven-plugin</artifactId>
    <version>0.9.5</version>
    <configuration>
        <packages>
            <package>com.test.model</package>
        </packages>
        <outputPath>${project.build.directory}/schema_MariaDb.sql</outputPath>
        <jpaProperties>
            <property>
                <name>hibernate.dialect</name>
                <value>org.hibernate.dialect.MySQL5InnoDBDialect</value>
            </property>
        </jpaProperties>
    </configuration>
</plugin>

And invoke with command: mvn jpa2ddl:generate

MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • thanks for the quick reply again I have been on other things... I have had a look at the github project and noticed a sequence skip was added. I have tried to use it but it still generates the sequence like so `create table UserSeq ( next_val bigint ) engine=InnoDB; insert into UserSeq values ( 2 );` I also tried the 'hibernate.id.new_generator_mappings=false' to no avail. I don't suppose you have any other suggestions? – David Rowntree Nov 08 '17 at 15:44
  • @DavidRowntree Do you use latest version 0.9.6? If yes could you please create the issue in the repository and provide some sample code? – Jakub Kubrynski Nov 08 '17 at 17:04
  • 2
    @JakubKubrynski that's exactly what it was, thank you very much it's much appreciated, great and useful tool. – David Rowntree Nov 09 '17 at 16:18