为什么我的schema.ddl在hibernate3-maven-plugin之后是空的? [英] 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-unit name =abc>
< jta-data-source> java:/ abcDS< / jta-data-source>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< / properties>
< / persistence-unit>
< /余辉>
这是我的 Abc.java
文件:
import javax.persistence。*;
@Entity
公共类Abc {
@Id private int id;
}
运行 mvn clean hibernate3:hbm2ddl
我得到这个输出:
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - 将
生成的模式写入文件:../target/hibernate3/sql/schema.ddl
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - 模式导出完成
[信息] ------------------------------------
[信息]建立成功
创建文件 schema.ddl
,它是空的。为什么?除此之外,我的配置文件有什么问题?因为当我试图用 PersistenceContext
注入运行单元测试时,它们以 NullPointerException
失败。看起来在配置中有一些问题。网上找不到任何手册...
PS。有两个问题,我已经找到它们了。第一个是这里(应该删除额外的前缀):
< property name =archive.autodetection> true< ; /性>
第二个更有趣。当我在编译后运行 mvn hibernate3:hbm2ddl
时它有效(因为它有 .class
文件可用) 。
有两个问题,我已经发现了它们。第一个是这里(额外的前缀应该被删除)
确实。所以我会跳过这个。
如何指示这个插件预先编译java类?
不可能(但是相反的方法是,即在 compile
之后运行插件,我们将会看到)。
事实上,Hibernate3 Maven Plugin早于注释,最初设计用于处理 hbm.xml
映射文件。这就是为什么 hibernate3:hbm2ddl
在执行自己之前调用生命周期阶段 process-resources
的执行。
当映射使用注释而不是XML文件时,目标确实必须在编译
阶段之后运行( process-classes code>
阶段将是一个自然的候选项),但这不是当前行为 hibernate3:hbm2ddl
。
因此,在调用目标之前,您必须运行 compile
:
mvn编译hibernate3:hbm2ddl
另一个选项是绑定 hibernate3:hbm2ddl
在构建生命周期中,例如on process-classes
:
< project>
...
< build>
< plugins>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> hibernate3-maven-plugin< / artifactId>
< version> 2.2< / version>
<执行次数>
<执行>
<阶段>过程类< /阶段><! - 编译也会起作用 - >
<目标>
< goal> hbm2ddl< / goal>
< /目标>
< /执行>
< /执行次数>
< / plugin>
< / plugins>
< / build>
...
< / project>
然后运行 process-classes
到触发插件:
mvn process-classes
This is the directory structure of the project (maven2 is used):
pom.xml
/src
/main
/java
Abc.java
/resources
hibernate.cfg.xml
database.properties
/META-INF
persistence.xml
/test
/java
AbcTest.java
/resources
database.properties
This is the content of hibernate.cfg.xml
:
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.archive.autodetection">true</property>
</session-factory>
</hibernate-configuration>
This is what I have in 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>
And this is my Abc.java
file:
import javax.persistence.*;
@Entity
public class Abc {
@Id private int id;
}
After running mvn clean hibernate3:hbm2ddl
I'm getting this output:
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
File schema.ddl
is created, and it's empty. Why? And besides that, what is wrong in my configuration files? Since when I'm trying to run unit tests with PersistenceContext
injections they fail with NullPointerException
. Looks like there is some problem in the configuration. Can't find any manual online…
PS. There are two problems, I found them already. The first one is here (extra prefix should be removed):
<property name="archive.autodetection">true</property>
The second one is more interesting. When I'm running mvn hibernate3:hbm2ddl
after compilation it works (because it has .class
files to work with). Otherwise the schema is empty.. How to instruct this plugin to compile java classes beforehand?
There are two problems, I found them already. The first one is here (extra prefix should be removed)
Indeed. So I'll skip this one.
How to instruct this plugin to compile java classes beforehand?
Not possible (but the other way around would be, i.e. running the plugin after compile
, as we'll see).
The fact is that the Hibernate3 Maven Plugin, which predates annotations, has been initially designed to deal with hbm.xml
mapping files. And that's why hibernate3:hbm2ddl
invokes the execution of the lifecycle phase process-resources
prior to executing itself.
When using annotation instead of XML files for the mappings, the goal would indeed have to run after the compile
phase (the process-classes
phase would be a natural candidate) but that's not the current behavior of hibernate3:hbm2ddl
.
So you'll have to run compile
before invoking the goal:
mvn compile hibernate3:hbm2ddl
The other option would be to bind the hibernate3:hbm2ddl
on the build lifecycle, e.g. on 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>
And then just run process-classes
to trigger the plugin:
mvn process-classes
这篇关于为什么我的schema.ddl在hibernate3-maven-plugin之后是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!