我想用hibernate从MySQL数据库中创建Java类。只要使用eclipse和Hibernate-Plugin,这个工作正常(在这里描述:http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables),但我想用maven来完成。经过一些尝试后,这不起作用。使用hibernate和maven生成Java数据库 - 缺少AnnotationConfiguration?
一般情况下,我有一个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插件的文档(http://mojo.codehaus.org/hibernate3-maven-plugin)。
我试了几个代码段,最有希望在我看来,从: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]
我GOOGLE了错误,并发现http://www.mkyong.com/hibernate/hibernate-error-an-annotationconfiguration-instance-is-required-to-use/,必须添加依赖项。这似乎在某种程度上尴尬给我,因为我使用Hibernate 4和Maven插件对Hibernate 3(休眠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错误。
如果有人想尝试一下:整个聚甲醛在这里:http://nopaste.info/a70449bee6.html。
谢谢你的提示,但不幸的是,它并没有改变任何东西(详见后编辑...)。 –