【问题标题】:configuring hyperjaxb to create hibernate mappings and a mysql database配置 hyperjaxb 以创建休眠映射和 mysql 数据库
【发布时间】:2014-12-03 17:24:55
【问题描述】:

我正在使用 hyperjaxb 从 xsd 文件生成 Java 类。如何配置它以生成休眠注释,并触发hbm2ddl 来创建一个 MySQL 数据库,其中包含生成的类的表?

我下载了 hibernate from this link 的采购订单示例,然后导航到 cmd.exe 中的目标目录并运行 mvn clean install,但生成的文件夹不包含任何 java 类,这也不包含任何 hibernate /MySQL。我想获得一个从下载的xsd 文件创建所有内容的工作示例,这样我就可以将自己的xsd 文件插入代码并自动生成所有java/hibernate/mysql。这样我就可以花时间调整我的xsd 文件,以便生成的 java/hibernate/MySQL 是我需要的。

代码示例或分步说明会很有帮助。我正在使用日食。


**编辑:**

这个问题的答案是在发布了其他几个问题后得出的。要找到此问题的完整答案,您需要查看其他问题的答案,特别是 the one at this link

【问题讨论】:

标签: java hibernate jaxb hbm2ddl hyperjaxb


【解决方案1】:

更新

尽管tutorial 明确说明了查找位置,但 OP 最终似乎在项目的根目录中寻找生成的类,而不是 target\generated-source\xjc

如果你浏览 target/generated-sources/xjc 目录,你会发现 生成的 java 文件很少,例如 PurchaseOrderType.java


当你说什么都没有生成时,我不明白。我刚刚重新检查过,一切正常。

我的步骤是:

  • 下载 hyperjaxb3-ejb-samples-po-initial-0.5.6-maven-src.zip
  • 解压
  • 转到 hyperjaxb3-ejb-samples-po-initial-0.5.6
  • mvn 全新安装

这是我得到的:

[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] ------------------------------------------------------------------------

完整的 mvn clean install -X 日志是here

所以我希望你看到,它工作得很好。请发帖mvn clean install -X,可能你做错了什么。

现在,关于您的问题,这里是 MySQL 示例:

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

本项目包含一个hbm2ddl生成的sn-p:

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

hbm2ddl由于某种原因被注释掉了,不确定它是否有效,但它应该给出方向。

【讨论】:

  • @CodeMed 什么???它确实会生成所有内容,往返测试运行良好,实际上您在哪里寻找课程?
  • 那么,看看target/generated-sources/xjc。 :) 教程中明确建议了这个目录:如果您浏览 target/generated-sources/xjc 目录,您会发现一些生成的 java 文件,例如 PurchaseOrderType.java。 confluence.highsource.org/display/HJ3/…?
  • 不确定您的意图是什么。示例在这里:github.com/highsource/hyperjaxb3/tree/master/ejb/samples,模板在这里:github.com/highsource/hyperjaxb3/tree/master/ejb/templates,但这些不是“普通”项目,它们是作为示例构建的。 IE。这些项目中的pom.xmls 只是使用示例内容构建.zips。您最好在github.com/highsource/hyperjaxb3/tree/master/ejb/tests 中查看可执行测试。
  • @CodeMed 不确定导入到 src/main/java 是什么意思。打开项目属性,选择 Java Build Path,点击 Source 选项卡,确保选中“Allow output folders for source folder”,点击 Add Folder... 按钮,展开“target”,展开“generated-source”,勾选“xjc”,点击确定,然后点击确定。这会将生成 Java 文件的目录添加到类路径中。
  • @CodeMed 这是我用来运行 hbm2ddl 的 maven 插件:de.smartics.maven.pluginhibernate4-maven-plugin0.2.0
【解决方案2】:

这是another answer 的一部分,它回答了 MySQL 部分。我在此处添加此内容以供将来参考,以尝试结束此问题。

所以从现在开始,我假设 PO tutorial 工作正常:代码已生成,往返测试使用 HSQLDB 数据库等运行。

现在我们将解决两个问题:

  • 如何切换到 MySQL?
  • 如何使用 hbm2ddl 生成数据库架构?

让我们开始吧。

切换到 MySQL

首先,您必须将pom.xml 中的HSQLDB 替换为MySQL。删除这个:

    <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。如果往返测试未注释掉且数据库可用,则往返测试应与 DB 一起运行,即创建架构、导入示例 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 中生成数据库模式。

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多