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
.