问题
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 ;-)
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/7498174/hbm2java-failedunable-to-load-class-declared-as-mapping-class-com-dss-domain