【发布时间】:2014-04-18 06:06:19
【问题描述】:
我想使用 hibernate 从 MySQL 数据库中创建 Java 类。只需使用 eclipse 和 Hibernate-Plugin,就可以正常工作(在此处描述:http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables),但我想用 maven 来做。经过一些尝试,这不起作用。
一般来说,我有一个 hibernate.cfg.xml 和一个 persistance.xml 文件,它们都带有正确的连接信息。我发现了一些关于如何从 java 生成类的线程(例如 How to configure hibernate-tools with maven to generate hibernate.cfg.xml, *.hbm.xml, POJOs and DAOs)和 hibernate-maven-plugin 的文档(http://mojo.codehaus.org/hibernate3-maven-plugin)。
我尝试了几个代码sn-ps,最有希望的似乎来自:Maven Java Source Code Generation for Hibernate
我添加了我需要的文件,我得到了:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>configuration</implementation>
<outputDirectory>target/generated-sources/hibernate3</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<configurationfile>/src/main/java/hibernate.cfg.xml</configurationfile>
<packagename>de.unileipzig.database</packagename>
</componentProperties>
</configuration>
</plugin>
但不幸的是,在执行时,我得到了
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (default-cli) on project AWV: Execution default-cli of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: An AnnotationConfiguration instance is required to use <mapping class="de.unileipzig.database.objectlist"/> -> [Help 1]
我用谷歌搜索了错误,并在http://www.mkyong.com/hibernate/hibernate-error-an-annotationconfiguration-instance-is-required-to-use/ 上发现必须添加依赖项。这对我来说似乎有点尴尬,因为我正在使用 Hibernate 4 和 Hibernate 3 的 Maven 插件(hibernate 4 插件似乎不适用于我的情况:http://www.smartics.eu/hibernate4-maven-plugin/),但我尝试添加:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
(因为 Mykong-Post 指定的版本在我的存储库中找不到)。
不幸的是,错误仍然发生。有没有人提示如何解决这个问题?只是注解依赖有问题,还是我对插件的使用不对?
在 julschi 的建议下,我在插件中添加了以下代码:
<plugin>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
</dependencies>
</plugin>
不幸的是,这并没有改变任何东西。当我使用我在项目中使用的版本(Hibernate 4.2.7)时,它会导致错误,即找不到 org.hibernate.util.StringHelper;它似乎被移到另一个包(https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/internal/util/StringHelper.html)。但是如果我使用 3.5.6-FINAL 版本,我只会得到相同的 AnnotationConfiguration 错误。
如果有人想尝试一下:整个 POM 都在这里:http://nopaste.info/a70449bee6.html。
【问题讨论】: