![](/img/trans.png)
[英]How to keep hibernate3-maven-plugin HBM2DDL from printing to console
[英]why my schema.ddl is empty after hibernate3-maven-plugin?
這是項目的目錄結構(使用maven2):
pom.xml
/src
/main
/java
Abc.java
/resources
hibernate.cfg.xml
database.properties
/META-INF
persistence.xml
/test
/java
AbcTest.java
/resources
database.properties
這是hibernate.cfg.xml
的內容:
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.archive.autodetection">true</property>
</session-factory>
</hibernate-configuration>
這是我在persistence.xml
:
<persistence>
<persistence-unit name="abc">
<jta-data-source>java:/abcDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
這是我的Abc.java
文件:
import javax.persistence.*;
@Entity
public class Abc {
@Id private int id;
}
運行mvn clean hibernate3:hbm2ddl
我得到了這個輸出:
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - writing
generated schema to file: ../target/hibernate3/sql/schema.ddl
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
[INFO] ————————————————————————————————————
[INFO] BUILD SUCCESSFUL
文件schema.ddl
已創建,它為空。 為什么? 除此之外,我的配置文件有什么問題? 因為當我嘗試使用PersistenceContext
注入運行單元測試時,它們會因NullPointerException
而失敗。 看起來配置中存在一些問題。 在網上找不到任何手冊......
PS。 有兩個問題,我已經找到了。 第一個在這里(應刪除額外的前綴):
<property name="archive.autodetection">true</property>
第二個更有趣。 當我在編譯后運行mvn hibernate3:hbm2ddl
它可以工作(因為它有.class
文件可以使用)。 否則架構為空。如何指示此插件事先編譯java類?
有兩個問題,我已經找到了。 第一個在這里(應刪除額外的前綴)
確實。 所以我會跳過這個。
如何指示這個插件事先編譯java類?
不可能(但另一種方式是,即在compile
后運行插件,我們將看到)。
事實上,早於注釋的Hibernate3 Maven插件最初設計用於處理hbm.xml
映射文件。 這就是為什么hibernate3:hbm2ddl
在執行自身之前調用生命周期階段process-resources
執行 。
當使用注釋而不是XML文件進行映射時,目標確實必須在compile
階段之后運行( process-classes
階段將是一個自然的候選者),但這不是hibernate3:hbm2ddl
的當前行為hibernate3:hbm2ddl
。
所以你必須在調用目標之前運行compile
:
mvn compile hibernate3:hbm2ddl
另一種選擇是在構建生命周期中綁定hibernate3:hbm2ddl
,例如在process-classes
:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>process-classes</phase><!-- compile would also work -->
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
然后只需運行process-classes
來觸發插件:
mvn process-classes
修復在其映射配置中將DTO注釋類添加到hibernate.cfg.xml。 這解決了問題並立即為我工作,生成了完整的數據庫模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.