我正在使用Hibernate3 Maven插件从数据库实现域/模型POJO的生成。基本原理是确保DBA对数据库的更新在开发人员开始进一步工作之前自动映射到模型层。因此它必须工作的方式是生成Hibernate CFG,然后生成POJO;此外,由于较旧的实现由使用注释而不是hbm.xml的开发人员组成,因此需要对生成的类进行注释。这是POM for 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失败并带有消息
无法执行目标 org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java(hbm2java)on project dss-domain:执行目标的hbm2java org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java失败:无法使用 加载声明为&lt;的类制图 class =“com.dss.domain.Foo”/&gt;在配置中: - &gt; [救命 1]
在稍后阶段,所有这些都必须移动我们当前拥有的JPA实现,所以另一个问题是我是否必须在组件属性中切换到jpaconfiguration?
如果我将依赖关系更新为旧项目中最近使用的依赖项(Hibernate 3.6.6-FINAL),那么这些似乎都不起作用。但这是一个单独的问题here。
非常欢迎任何指针或完整的解决方案; - )
答案 0 :(得分:1)
我正在使用带有maven构建的mysql的hibernate。我没有运行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
答案 1 :(得分:0)
我正在浏览并看到一个类似的帖子(不知道我是如何在第一时间错过它)但是无论如何,当我在我的构建中添加额外的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中以映射和注释配置的形式复制信息?让我对这个插件比以前更加困惑。