hbm2java 失败:无法加载声明为的类

发布于 2024-12-06 06:05:22 字数 2969 浏览 1 评论 0原文

我正在使用 Hibernate3 Maven 插件从数据库实现域/模型 POJO 的生成。其基本原理是确保 DBA 对数据库的更新在开发人员开始处理进一步的事情之前自动映射到模型层。所以它的工作方式是生成 Hibernate CFG,然后生成 POJO;另外,由于较旧的实现由使用注释而不是 hbm.xml 的开发人员组成,因此需要对生成的类进行注释。这是 Hibernate Maven 插件 POM 的摘录,

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <id>hbm2cfgxml</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>hbm2cfgxml</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2cfgxml</name>
              <implementation>jdbcconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <ejb3>true</ejb3>
            <packagename>com.dss.domain</packagename>
          </componentProperties>
        </configuration>
      </execution>
      <execution>
        <id>hbm2java</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>hbm2java</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2java</name>
              <implementation>annotationconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <ejb3>true</ejb3>
            <packagename>com.dss.domain</packagename>
            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
          </componentProperties>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.16</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

我可以看到生成了 cfg.xml 文件;但 hbm2java 失败并显示消息

执行目标失败 org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (hbm2java) 上 项目dss-domain:目标的执行hbm2java org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java 失败: 无法 加载声明为 < 的类映射 类=“com.dss.domain.Foo”/>在配置中:-> [帮助 1]

在稍后阶段,所有这些都必须转移到我们当前拥有的 JPA 实现中,因此另一个问题是我是否必须切换到组件属性中的 jpaconfiguration?

另外,如果我将依赖项更新为旧项目(Hibernate 3.6.6-FINAL)中最近更新的依赖项,这些似乎都不起作用;但这是一个单独的问题

非常欢迎任何指示或完整的解决方案;-)

I am implementing generation of the domain/model POJOs from database using the Hibernate3 Maven Plugin. The rationale is to ensure a DBA's updates to the database are automatically mapped to the model layer before a developer starts working on further things. So the way it has to work is that a Hibernate CFG is generated and then POJOs; also since the older implementation consisted of developers using annotations instead of hbm.xml the generated classes are required to be annotated. Here's extract from the POM for Hibernate Maven Plugin

<build>
<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <id>hbm2cfgxml</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>hbm2cfgxml</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2cfgxml</name>
              <implementation>jdbcconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <ejb3>true</ejb3>
            <packagename>com.dss.domain</packagename>
          </componentProperties>
        </configuration>
      </execution>
      <execution>
        <id>hbm2java</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>hbm2java</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2java</name>
              <implementation>annotationconfiguration</implementation>
            </component>
          </components>
          <componentProperties>
            <ejb3>true</ejb3>
            <packagename>com.dss.domain</packagename>
            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
          </componentProperties>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.16</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

I can see the cfg.xml file is generated; but hbm2java fails with message

Failed to execute goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (hbm2java) on
project dss-domain: Execution hbm2java of goal
org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: Unable
to load class declared as < mapping
class="com.dss.domain.Foo" / > in the configuration: -> [Help
1]

At a later stage all of this has to be moved the JPA implementation that we currently have, so the other question is do I then have to switch to jpaconfiguration in component properties?

Also none of these seems to work at all if I update the dependencies to the ones recently uopdated in the older project (Hibernate 3.6.6-FINAL); but that's a separate question posted here.

Any pointers or complete solutions are very welcome ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-12-13 06:05:22

我正在使用 hibernate 和用 maven 构建的 mysql。我没有运行 hbm2hbmxml,而是将执行目标更改为仅运行 hbm2cfgxml 和 hbm2java。现在我的项目生成基于注释的 pojos 和 hibernate.cfg.xml。

希望这有帮助!

查看我的配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springpress</groupId>
    <artifactId>hibernate</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hibernate</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.19</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
            <!-- will come with all needed Spring dependencies such as spring-core 
            and spring-beans -->
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.1.Final</version>
            <!-- will come with Hibernate core -->
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>generate-xml-files</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <!--  <goal>hbm2hbmxml</goal> -->
                            <goal>hbm2cfgxml</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-entities</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>hbm2java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <components>
                        <component>
                            <name>hbm2hbmxml</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/classes</outputDirectory>
                        </component>
                        <component>
                            <name>hbm2cfgxml</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/classes</outputDirectory>
                        </component>
                        <component>
                            <name>hbm2java</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/generated-sources/hibernate</outputDirectory>
                        </component>
                    </components>
                    <componentProperties>
                        <propertyfile>src/main/resources/hibernate.properties</propertyfile>
                        <jdk5>true</jdk5>
                        <ejb3>true</ejb3>
                        <packagename>com.springpress.hibernate.entities</packagename>
                        <format>true</format>
                        <haltonerror>true</haltonerror>
                    </componentProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>cglib</groupId>
                        <artifactId>cglib-nodep</artifactId>
                        <version>2.2.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.19</version>
                    </dependency></dependencies>
                </plugin>
            </plugins>
        </build>
    </project>

我有 hibernate.properties,例如:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mydb
hibernate.connection.username=root
hibernate.connection.password=pass
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.default_schema=mydb

I am using hibernate with mysql built with maven. Instead of running hbm2hbmxml I have changed my execution goals to only run hbm2cfgxml and hbm2java. Now my project generates annotation based pojos and hibernate.cfg.xml.

Hope this helps!

See my configuration:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springpress</groupId>
    <artifactId>hibernate</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hibernate</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.19</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
            <!-- will come with all needed Spring dependencies such as spring-core 
            and spring-beans -->
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.1.Final</version>
            <!-- will come with Hibernate core -->
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>generate-xml-files</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <!--  <goal>hbm2hbmxml</goal> -->
                            <goal>hbm2cfgxml</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-entities</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>hbm2java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <components>
                        <component>
                            <name>hbm2hbmxml</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/classes</outputDirectory>
                        </component>
                        <component>
                            <name>hbm2cfgxml</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/classes</outputDirectory>
                        </component>
                        <component>
                            <name>hbm2java</name>
                            <implementation>jdbcconfiguration</implementation>
                            <outputDirectory>target/generated-sources/hibernate</outputDirectory>
                        </component>
                    </components>
                    <componentProperties>
                        <propertyfile>src/main/resources/hibernate.properties</propertyfile>
                        <jdk5>true</jdk5>
                        <ejb3>true</ejb3>
                        <packagename>com.springpress.hibernate.entities</packagename>
                        <format>true</format>
                        <haltonerror>true</haltonerror>
                    </componentProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>cglib</groupId>
                        <artifactId>cglib-nodep</artifactId>
                        <version>2.2.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.19</version>
                    </dependency></dependencies>
                </plugin>
            </plugins>
        </build>
    </project>

And I have hibernate.properties like:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mydb
hibernate.connection.username=root
hibernate.connection.password=pass
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.default_schema=mydb
ペ泪落弦音 2024-12-13 06:05:22

我正在浏览并看到类似的帖子(不知道我一开始是如何错过它的),但无论如何,当我在构建中添加额外的 hbm2hbmxml 时;构建不会因错误而失败

      <execution>
        <id>hbm2hbmxml</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>hbm2hbmxml</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2hbmxml</name>
              <outputDirectory>target/classes</outputDirectory>
            </component>
          </components>
          <componentProperties>
            <packagename>com.sapient.dss.dbci.domain</packagename>
          </componentProperties>
        </configuration>
      </execution>

但这不是我正在寻找的解决方案。当我看到 hibernate.cfg.xml 时,它正在使用指向 .hbm.xmls 的映射资源;并且生成的java源使用JPA注释!!!

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/liquibrain</property>
        <property name="hibernate.connection.username">liquibrain</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping resource="com/dss/domain/Foo.hbm.xml" />
        <mapping resource="com/dss/domain/Bar.hbm.xml" />
    </session-factory>
</hibernate-configuration>

,这里是生成的 Java 源代码的摘录:

/**
 * Foo generated by hbm2java
 */
@Entity
@Table(name="iteration"
    ,catalog="liquibrain"
)
public class Foo implements java.io.Serializable {
...
...
@Id @GeneratedValue(strategy=IDENTITY)

@Column(name="id", nullable=false)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}
...
...
...
@ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="bar_foos", joinColumns = { 
        @JoinColumn(name="foo_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
        @JoinColumn(name="bar_id", nullable=false, updatable=false) })
    public Set getBars() {
        return this.bars;
    }

hbm 文件和 java 源代码都打包在 JAR 中,但由于 hibernate.cfg.xml 提到通过 .hbm.xml 进行映射,我相信这就是它的方式被转介。那么,是否有一种方法可以生成 java 源代码,而无需在 POJO 中以映射和注释配置的形式复制信息呢?让我现在比以前更困惑这个插件了。

I was browsing through and saw a similar post (not sure how I missed it in the first place) but anyways, when I add an additional hbm2hbmxml to my build; the build does not fail in error

      <execution>
        <id>hbm2hbmxml</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>hbm2hbmxml</goal>
        </goals>
        <inherited>false</inherited>
        <configuration>
          <components>
            <component>
              <name>hbm2hbmxml</name>
              <outputDirectory>target/classes</outputDirectory>
            </component>
          </components>
          <componentProperties>
            <packagename>com.sapient.dss.dbci.domain</packagename>
          </componentProperties>
        </configuration>
      </execution>

But this is not the solution I am looking for. When I see hibernate.cfg.xml it is using mapping resources pointing to .hbm.xmls; and the generated java sources are using JPA annotations!!!

the hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/liquibrain</property>
        <property name="hibernate.connection.username">liquibrain</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping resource="com/dss/domain/Foo.hbm.xml" />
        <mapping resource="com/dss/domain/Bar.hbm.xml" />
    </session-factory>
</hibernate-configuration>

and here's an extract from the generated Java source:

/**
 * Foo generated by hbm2java
 */
@Entity
@Table(name="iteration"
    ,catalog="liquibrain"
)
public class Foo implements java.io.Serializable {
...
...
@Id @GeneratedValue(strategy=IDENTITY)

@Column(name="id", nullable=false)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}
...
...
...
@ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="bar_foos", joinColumns = { 
        @JoinColumn(name="foo_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
        @JoinColumn(name="bar_id", nullable=false, updatable=false) })
    public Set getBars() {
        return this.bars;
    }

Both the hbm files and java sources get packaged in the JAR, but since the hibernate.cfg.xml mentions mapping through .hbm.xml I belibe thats how it will be reffered. So isn't there a way to generate the java source without having to duplicate the info in form of both mappings and annotation configurations in POJOs? Makes me more confused about the plugin now than before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文