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.
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:I don't understand it when you say that nothing is generated. I've just rechecked it, everything works fine.
My steps are:
Here's what I get:
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.
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:
Let's get started.
Switching to MySQL
First of all, you have to replace HSQLDB with MySQL in the
pom.xml
. Remove this:And add this:
Next, edit
src/test/resources/persistence.properties
. Replace this:With this:
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
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 yourpom.xml
(project/build/plugins
):Few things are important:
jpaconfiguration
.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).${project.build.directory}/classes
) to the hibernatetool's classpath so that it can discover classes and read annotations.propertyfile="src/test/resources/persistence.properties"
).persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"
). Take a look attarget/generated-sources/xjc/META-INF/persistence.xml
.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
.