这是另一个回答MySQL 部分的答案的一部分。我在此处添加此内容以供将来参考,以尝试结束此问题。
所以从现在开始,我假设PO 教程工作正常:生成代码,使用 HSQLDB 数据库运行往返测试等。
现在我们将解决两个问题:
- 如何切换到 MySQL?
- 如何使用 hbm2ddl 生成数据库模式?
让我们开始吧。
切换到 MySQL
首先,您必须在pom.xml
. 删除这个:
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
</dependency>
并添加:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
<scope>test</scope>
</dependency>
接下来,编辑src/test/resources/persistence.properties
. 替换这个:
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
有了这个:
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
我个人目前手头没有 MySQL 数据库,所以我无法真正测试往返。因此我会注释掉
<!--roundtripTestClassName>RoundtripTest</roundtripTestClassName-->
在pom.xml
.
如果您手头有数据库,只需在上述persistence.properties
文件中配置正确的 URL/用户名/密码。
此时,您的 Maven 项目已重新配置为使用 MySQL。如果没有注释掉往返测试并且数据库可用,那么往返测试应该与数据库一起运行,即创建模式、导入示例 XML、读回并比较 alpha 和 omega。
所以现在我们有了关于 MySQL 的教程,可以继续学习了。
生成数据库模式
这是一个棘手的部分。
为了在文件中生成数据库模式,您必须使用该hbm2ddl
工具。有 Maven 插件,在 Hibernate 3 的情况下,Codehaus 插件似乎是领先的插件。最后,我想出了以下配置。您必须将以下插件添加到您的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>
有几件事很重要:
- Hyperjaxb3 生成 JPA 注释,因此您必须使用
jpaconfiguration
.
- 因此,
hibernate3-maven-plugin
必须在编译阶段执行(您需要类来读取注释,因此必须在那时编译它们)。
- 您必须将已编译的类 (
${project.build.directory}/classes
) 包含到 hibernatetool 的类路径中,以便它可以发现类并读取注释。
- 你必须让 hibernatetool 知道你在哪里可以找到你的 Hibernate 属性 (
propertyfile="src/test/resources/persistence.properties"
)。
- 最后你必须让它知道你想要处理哪个持久化单元(
persistenceunit="org.jvnet.hyperjaxb3.ejb.tests.pocustomized"
)。看看target/generated-sources/xjc/META-INF/persistence.xml
。
- 最后,添加所有必需的依赖项。
最后,您到达了我在上面发布的配置。此时,构建还应该在target/sql/hibernate3/schema.ddl
.