我想用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。
謝謝你的提示,但不幸的是,它並沒有改變任何東西(詳見後編輯...)。 –