我正在使用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在消息中失败了
项目dss域上的目标org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java ( hbm2java )执行失败:目标org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java的执行hbm2java失败:无法加载配置:->帮助1中声明为< Help class="com.dss.domain.Foo“/>的类
在稍后阶段,所有这些都必须移动我们目前拥有的JPA实现,因此另一个问题是,我是否必须切换到组件属性中的jpaconfiguration?
而且,如果我将依赖项更新到最近更新到旧项目(Hibernate 3.6.6-FINAL)中的依赖项,所有这些似乎都不起作用;但这是here发布的另一个问题。
任何指针或完整的解决方案都非常欢迎;)
发布于 2012-05-03 23:28:25
我在用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发布于 2011-09-24 09:39:39
在浏览时,我看到了一个类似的帖子(最初不确定我是如何错过它的),但是无论如何,当我向我的构建中添加一个额外的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注释!
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>下面是从生成的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中以映射和注释配置的形式复制信息吗?比以前更让我对这个插件感到困惑了。
https://stackoverflow.com/questions/7498174
复制相似问题