1

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

<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 失败并显示消息

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

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

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

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

4

2 回答 2

2

我正在使用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
于 2012-05-03T23:28:25.193 回答
1

我正在浏览并看到一个类似的帖子(不知道我是如何错过它的),但无论如何,当我在我的构建中添加一个额外的 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 中以映射和注释配置的形式复制信息?现在让我对插件比以前更加困惑。

于 2011-09-24T09:39:39.800 回答