gpt4 book ai didi

java - 为什么我的 schema.ddl 在 hibernate3-maven-plugin 之后是空的?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:40:18 24 4
gpt4 key购买 nike

这是项目的目录结构(使用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 注入(inject)运行单元测试时,它们因 NullPointerException 而失败。看起来配置中存在一些问题。在网上找不到任何手册……

PS. 有两个问题,我已经找到了。第一个在这里(应该删除额外的前缀):

<property name="archive.autodetection">true</property>

第二个更有趣。当我在编译后运行 mvn hibernate3:hbm2ddl 时,它可以工作(因为它有 .class 文件可以使用)。否则模式为空。如何指示此插件预先编译 java 类?

最佳答案

There are two problems, I found them already. The first one is here (extra prefix should be removed)

的确如此。所以我会跳过这个。

How to instruct this plugin to compile java classes beforehand?

不可能(但反之亦然,即在 compile 之后运行插件,我们将看到)。

事实上,早于注释的 Hibernate3 Maven Plugin 最初设计用于处理 hbm.xml 映射文件。这就是为什么 hibernate3:hbm2ddl 在执行自身之前调用生命周期阶段 process-resources 的执行

当使用注释而不是 XML 文件进行映射时,目标确实必须在 compile 阶段之后运行(process-classes 阶段将是一个自然的候选者)但这不是当前行为hibernate3:hbm2ddl

所以你必须在调用目标之前运行编译:

mvn compile hibernate3:hbm2ddl

另一种选择是在构建生命周期中绑定(bind) hibernate3:hbm2ddl,例如关于进程类:

<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

关于java - 为什么我的 schema.ddl 在 hibernate3-maven-plugin 之后是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3805983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com