1

I am using hyperjaxb to generate Java classes from an xsd file. How can I configure it to generate hibernate annotations, and to trigger hbm2ddl to create a MySQL database with tables for the generated classes?

I downloaded the purchase order sample for hibernate from this link, then navigated to the target directory in cmd.exe and ran mvn clean install, but the resulting folders did not contain any java classes, and this also did not contain any hibernate/MySQL. I would like to get a working example that creates everything from a downloaded xsd file so that I can just plug my own xsd file into the code and have all the java/hibernate/mysql be autogenerated. That way I can spend my time tweaking my xsd file so that the resulting java/hibernate/MySQL is what I need it to be.

A code example or step by step instructions would be really helpful. I am using eclipse.


**EDIT: **

The answer to this question came after posting a few other questions. To find the complete answer to this question, you will need to review the answers to those other questions, in particular, the one at this link.

Community
  • 1
  • 1
CodeMed
  • 9,527
  • 70
  • 212
  • 364

2 Answers2

4

UPDATE

Finally it appeared that the OP was looking for generated classes in the root directory of the project insteadof target\generated-source\xjc, despite the tutorial clearly states where to look:

If you browse the target/generated-sources/xjc directory, you'll find few generated java files, for instance PurchaseOrderType.java.


I don't understand it when you say that nothing is generated. I've just rechecked it, everything works fine.

My steps are:

  • Download hyperjaxb3-ejb-samples-po-initial-0.5.6-maven-src.zip
  • Unzip
  • Go to hyperjaxb3-ejb-samples-po-initial-0.5.6
  • mvn clean install

Here's what I get:

[INFO] ------------------------------------------------------------------------
[INFO] Building Hyperjaxb3 Samples [po-initial:maven] 0.5.6
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[INFO] Deleting C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target
[INFO] 
[INFO] --- maven-hyperjaxb3-plugin:0.6.0:generate (default) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[INFO] Sources are not up-to-date; XJC execution will be executed.
[WARNING] According to the Java Persistence API specification, section 2.1, entities must be top-level classes:
"The entity class must be a top-level class."
Your JAXB model is not customized as with top-level local scoping, please use the <jaxb:globalBinding localScoping="toplevel"/> global bindings customization.
        org.jvnet.hyperjaxb3.ejb.plugin.EjbPlugin

[WARNING] According to the Java Persistence API specification, section 2.1, entities must implement the serializable interface:
"If an entity instance is to be passed by value as a detached object
(e.g., through a remote interface), the entity class must implement
 the Serializable interface."
Your JAXB model is not customized as serializable, please use the <jaxb:serializable/> global bindings customization element to make your model serializable.
        org.jvnet.hyperjaxb3.ejb.plugin.EjbPlugin

[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[INFO] Surefire report directory: C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running RoundtripTest
Detected [file:/C:/Projects/workspaces/hj3/dist/hyperjaxb3-ejb-samples-po-initial-0.5.6/target/classes/META-INF/persistence.xml].
    RoundtripTest

Loading entity manager factory properties.
    RoundtripTest

Loading entity manager factory properties from [file:/C:/Projects/workspaces/hj3/dist/hyperjaxb3-ejb-samples-po-initial-0.5.6/target/test-classes/persistence.properties].
    RoundtripTest

Testing samples.
    RoundtripTest

Sample directory [C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\src\test\samples].
    RoundtripTest

Testing sample [po.xml].
    RoundtripTest

Unmarshalling.
    RoundtripTest

Opening session.
    RoundtripTest

Saving the object.
    RoundtripTest

Opening session.
    RoundtripTest

Loading the object.
    RoundtripTest

Closing the session.
    RoundtripTest

Initial object:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purchaseOrder orderDate="1999-10-20">
    <shipTo country="US">
        <name>Alice Smith</name>
        <street>123 Maple Street</street>
        <city>Mill Valley</city>
        <state>CA</state>
        <zip>90952</zip>
    </shipTo>
    <billTo country="US">
        <name>Robert Smith</name>
        <street>8 Oak Avenue</street>
        <city>Old Town</city>
        <state>PA</state>
        <zip>95819</zip>
    </billTo>
    <comment>Hurry, my lawn is going wild!</comment>
    <items>
        <item partNum="872-AA">
            <productName>Lawnmower</productName>
            <quantity>1</quantity>
            <USPrice>148.95</USPrice>
            <comment>Confirm this is electric</comment>
        </item>
        <item partNum="926-AA">
            <productName>Baby Monitor</productName>
            <quantity>1</quantity>
            <USPrice>39.98</USPrice>
            <shipDate>1999-05-21</shipDate>
        </item>
    </items>
</purchaseOrder>

    RoundtripTest

Source object:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purchaseOrder orderDate="1999-10-20" Hjid="1">
    <shipTo country="US" Hjid="2">
        <name>Alice Smith</name>
        <street>123 Maple Street</street>
        <city>Mill Valley</city>
        <state>CA</state>
        <zip>90952</zip>
    </shipTo>
    <billTo country="US" Hjid="1">
        <name>Robert Smith</name>
        <street>8 Oak Avenue</street>
        <city>Old Town</city>
        <state>PA</state>
        <zip>95819</zip>
    </billTo>
    <comment>Hurry, my lawn is going wild!</comment>
    <items Hjid="1">
        <item partNum="872-AA" Hjid="1">
            <productName>Lawnmower</productName>
            <quantity>1</quantity>
            <USPrice>148.95</USPrice>
            <comment>Confirm this is electric</comment>
        </item>
        <item partNum="926-AA" Hjid="2">
            <productName>Baby Monitor</productName>
            <quantity>1</quantity>
            <USPrice>39.98</USPrice>
            <shipDate>1999-05-21</shipDate>
        </item>
    </items>
</purchaseOrder>

    RoundtripTest

Result object:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purchaseOrder orderDate="1999-10-20" Hjid="1">
    <shipTo country="US" Hjid="2">
        <name>Alice Smith</name>
        <street>123 Maple Street</street>
        <city>Mill Valley</city>
        <state>CA</state>
        <zip>90952</zip>
    </shipTo>
    <billTo country="US" Hjid="1">
        <name>Robert Smith</name>
        <street>8 Oak Avenue</street>
        <city>Old Town</city>
        <state>PA</state>
        <zip>95819</zip>
    </billTo>
    <comment>Hurry, my lawn is going wild!</comment>
    <items Hjid="1">
        <item partNum="872-AA" Hjid="1">
            <productName>Lawnmower</productName>
            <quantity>1</quantity>
            <USPrice>148.95</USPrice>
            <comment>Confirm this is electric</comment>
        </item>
        <item partNum="926-AA" Hjid="2">
            <productName>Baby Monitor</productName>
            <quantity>1</quantity>
            <USPrice>39.98</USPrice>
            <shipDate>1999-05-21</shipDate>
        </item>
    </items>
</purchaseOrder>

    RoundtripTest

Checking the document identity.
    RoundtripTest

Finished testing sample [po.xml].
    RoundtripTest

Finished testing samples.
    RoundtripTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.552 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[INFO] Building jar: C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target\hyperjaxb3-ejb-samples-po-initial-maven-0.5.6.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hyperjaxb3-ejb-samples-po-initial-maven ---
[INFO] Installing C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\target\hyperjaxb3-ejb-samples-po-initial-maven-0.5.6.jar to C:\Repository\org\jvnet\hyperjaxb3\hyperjaxb3-ejb-samples-po-initial-maven\0.5.6\hyperjaxb3-ejb-samples-po-initial-maven-0.5.6.jar
[INFO] Installing C:\Projects\workspaces\hj3\dist\hyperjaxb3-ejb-samples-po-initial-0.5.6\pom.xml to C:\Repository\org\jvnet\hyperjaxb3\hyperjaxb3-ejb-samples-po-initial-maven\0.5.6\hyperjaxb3-ejb-samples-po-initial-maven-0.5.6.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.100 s
[INFO] Finished at: 2014-10-09T00:42:10+01:00
[INFO] Final Memory: 22M/96M
[INFO] ------------------------------------------------------------------------

Full mvn clean install -X log is here.

So I hope you see, it works perfectly. Please post you mvn clean install -X, maybe you do something wrong.

Now, concerning your question, here's the MySQL example:

https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/po-mysql

This project includes a snippet of hbm2ddl generation:

https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/issues

The hbm2ddl is commented out for some reason, not sure if it works, but it should give the direction.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • @CodeMed What??? It does generate everything, roundtrip test runs fine, where are you looking for classes, actually? – lexicore Oct 08 '14 at 23:00
  • Well, take a look in `target/generated-sources/xjc` then. :) This directory is clearly suggested by the tutorial: If you browse the target/generated-sources/xjc directory, you'll find few generated java files, for instance PurchaseOrderType.java. http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial#PurchaseOrderTutorial-Whatwasgenerated? – lexicore Oct 08 '14 at 23:12
  • Not sure what your intention is. Samples are here: https://github.com/highsource/hyperjaxb3/tree/master/ejb/samples, templates are here: https://github.com/highsource/hyperjaxb3/tree/master/ejb/templates, but these are not "normal" projects, they are build as examples. I.e. `pom.xml`s of these projects just build `.zip`s with example contents. You better look in https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests for executable tests. – lexicore Oct 08 '14 at 23:50
  • @CodeMed Not sure what you mean by import into src/main/java. Open your project properties, select Java Build Path, hit the Source tab, make sure "Allow output folders for source folders" is check, hit the Add Folder... button, expand "target", expand "generated-source", check "xjc", hit OK, and hit OK. This adds the directory where the Java files are generated to the classpath. – Rob Oct 09 '14 at 03:07
  • @CodeMed This is the maven plugin I am using for running hbm2ddl: de.smartics.maven.plugin hibernate4-maven-plugin 0.2.0 – Rob Oct 09 '14 at 03:10
  • @CodeMed This question is answered. If you have a further question on Eclipse usage, please ask it as a separate question. It surely does work in Eclipse. – lexicore Oct 09 '14 at 07:18
  • This is absolutely standard Maven directory structure, perfectly compatible with Eclipse/m2e. All of the conventional Maven code generation plugins generate code in `target\generated-sources\something`. Let me know if you want my honest feedback on your efforts. – lexicore Oct 09 '14 at 20:08
  • @lexicore even when I resort to putting my `Main.java` class in `target/generated-sources/xjc`, I still get `Launch Error: Selection does not contain a main type` when I try to run the code from your tutorial as a java application. By contrast, I am able to run a similar `Main.java` in the eclipse project that I used to import JAXB. You are using different directory structure than Oracle uses. – CodeMed Oct 09 '14 at 21:02
  • I'm sorry, but statements "your directory structure is not congruent with the way eclipse projects get set up" and "you are using different directory structure than Oracle uses" are not correct. Let me answer the other question, I'll explain. – lexicore Oct 09 '14 at 21:19
  • @lexicore What I mean is that I was able to easily plug the JAXB-generated code into a variety of different types of eclipse projects without issue. By contrast, the hyperjaxb-generated code throws errors when I import it into each of the same types of eclipse projects that I have tried. I do not mean to criticize you or your promising project. I am simply an eclipse user who is used to being able to plug things into eclipse without issue. I will look forward to learning from your answer to the other question. Thank you again. – CodeMed Oct 09 '14 at 22:04
  • My difficulty is that you seem to lack basic concepts of Maven projects. You just do plain wrong things. – lexicore Oct 09 '14 at 22:11
  • I've just posted my answer. No, you're wrong about IDE. In professional projects build scripts (be it Ant, Maven or whatever else) are obligatory and primary. IDE is completely secondary. You don't even have to have one. Want a proof? Go find open-source projects with no build files (`pom.xml` etc.) but with IDE files checked into the SCM. You'll hardly find any. One-step builds are essential. – lexicore Oct 09 '14 at 23:33
  • The answer to this question requires integrating answers to a few different questions, including this one: http://stackoverflow.com/questions/26366556/no-persistence-provider-for-entitymanager-named-persistence-xml/26366709?noredirect=1#comment41410137_26366709 – CodeMed Oct 15 '14 at 17:32
1

This is a part of another answer which answers the MySQL part. I add this here for future reference in an attempt to close this question.

So from now on I assume that the PO tutorial worked fine: the code was generated, roundtrip test ran with the HSQLDB database etc.

Now we'll address two questions:

  • How to switch to MySQL?
  • How to generate database schema with hbm2ddl?

Let's get started.

Switching to MySQL

First of all, you have to replace HSQLDB with MySQL in the pom.xml. Remove this:

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.7</version>
        <scope>test</scope>
    </dependency>

And add this:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
        <scope>test</scope>
    </dependency>

Next, edit src/test/resources/persistence.properties. Replace this:

hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url=jdbc:hsqldb:target/test-database/database
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0

With this:

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=...
hibernate.connection.password=...
hibernate.connection.url=jdbc:mysql://localhost/hj3
hibernate.hbm2ddl.auto=create-drop
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0

I personally don't have a MySQL database at hand at the moment, so I can't really test the roundtrip. Therefore I'll comment out

<!--roundtripTestClassName>RoundtripTest</roundtripTestClassName-->

in pom.xml.

If you have a database at hand, just configure the right URL/username/password in the mentioned persistence.properties file.

At this point your Maven project is reconfigured to use MySQL. If the roundtrip test is not commented out and the database is available, the roundrip test should run with the DB, i.e. create the schema, import the sample XML, read it back and compare alpha and omega.

So now we have the tutorial on MySQL and can move on.

Generating the database schema

This was a tricky part to figure out.

In order to generate the database schema in a file, you have to use the hbm2ddl tool. There are Maven plugins for that, in case of Hibernate 3 it seemed that the Codehaus plugin is the leading one. Finally, I have figured out the following configuration. You have to add the following plugin to your pom.xml (project/build/plugins):

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>3.0</version>
            <executions>
                <execution>
                    <id>generate-schema</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <hibernatetool>
                    <classpath>
                        <path location="${project.build.directory}/classes" />
                    </classpath>

                    <jpaconfiguration persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized" propertyfile="src/test/resources/persistence.properties"/>

                    <hbm2ddl export="false" create="true" update="false" format="true" outputfilename="schema.ddl" />

                </hibernatetool>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate.javax.persistence</groupId>
                    <artifactId>hibernate-jpa-2.0-api</artifactId>
                    <version>1.0.0.Final</version>
                </dependency>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <version>3.6.5.Final</version>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.5</version>
                </dependency>
            </dependencies>
        </plugin>

Few things are important:

  • Hyperjaxb3 generates JPA annotations, so you have to use jpaconfiguration.
  • Therefore the hibernate3-maven-plugin must be executed in the compile phase (you need classes to read annotations from so they have to be compiled at that moment).
  • You have to include the compiled classes (${project.build.directory}/classes) to the hibernatetool's classpath so that it can discover classes and read annotations.
  • You have to let the hibernatetool know where you find your Hibernate properties (propertyfile="src/test/resources/persistence.properties").
  • Finally you have to let it know, which persistence unit you want to process (persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"). Take a look at target/generated-sources/xjc/META-INF/persistence.xml.
  • Finally, add all the required dependencies.

Finally you arrive at the configuration I posted above. At this point the build should also generate the database schema in target/sql/hibernate3/schema.ddl.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221