I have search for this every where but couldnt find the answere i m following this reference and used this as a tutorial to write an ANT task to convert a POJO with ejb 3 annotations to a hibernate mapping file (hmb.xml)
My build ends successfully without errors but i cant found any hbm.xmk inside the target directory or any where
here is the code i used.
build.xml
<project basedir="." default="init" name="Test">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries">
<compilerarg value="-Xlint"/>
</javac>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<java fork="true" classname="uk.co.pookey.hibernate.HibernateUtil" classpathref="libraries">
<classpath path="${targetdir}"/>
<arg value="${action}"/>
</java>
</target>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="libraries" />
<target name="init" depends="clean, compile">
<hibernatetool destdir="${targetdir}">
<classpath>
<!-- it is in this classpath you put your classes dir, and/or jpa persistence compliant jar -->
<path location="${basedir}/bin" />
</classpath>
<annotationconfiguration configurationfile="${basedir}/src/hibernate.cfg.xml" propertyfile="${basedir}/src/hibernate.properties"/>
<hbm2dao destdir="${targetdir}"/>
</hibernatetool>
</target>
POJO - Blog.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="blog")
public class Blog {
@Id
@Column(name="id")
private Long id;
@Col开发者_如何学Goumn(name="subject")
private String subject;
@Column(name="body")
private String body;
@Column(name="creatdate")
private Date createdAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
}
OUT PUT
init:
[hibernatetool] Executing Hibernate Tool with a Hibernate Annotation/EJB3 Configuration
[hibernatetool] 1. task: hbm2dao (Generates a set of DAOs)
[hibernatetool] 118 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
[hibernatetool] 123 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.6.Final
[hibernatetool] 125 [main] INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.show_sql=false, hibernate.connection.url=jdbc:mysql://localhost:3306/sahan, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
[hibernatetool] 127 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
[hibernatetool] 132 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[hibernatetool] 226 [main] INFO org.hibernate.cfg.Configuration - configuring from file: hibernate.cfg.xml
[hibernatetool] 308 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: java:hibernate/SessionFactory
[hibernatetool] 340 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
[hibernatetool] Jul 29, 2011 2:55:16 PM org.hibernate.tool.Version <clinit>
[hibernatetool] INFO: Hibernate Tools 3.2.4.GA
and the Jar files i used are
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
freemarker-2.3.1.jar
hibernate-tools-3.2.4.GA.jar
hibernate3.jar
javassist-3.12.0.GA.jar
mysql-connector-java-5.0.8-bin.jar
slf4j-api-1.5.8.jar
slf4j-simple-1.5.8.jar
Please help me there is not much tutorials or working example sample codes for this task specially for generating hbm files from POJO s using annotationsconfiguration
Why would you want to convert from annotations to XML config? Many EJB3 annotations have direct Hibernate equivalents, or actually use the same annotation (javax.persistence.*
). I would just set up your project to compile against Hibernate and see what blows up, fix them, and move on. Then you don't need to the XML files.
Ok i have found the reason :D i didnt add mapping to the class in hibernate.cfg.xml file
previously it was
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.session_factory_name">jndi/composite/name</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
But it should be
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.session_factory_name">jndi/composite/name</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files of the class should go here -->
<mapping class="Blog"/>
</session-factory>
</hibernate-configuration>
what is missing was "" part now it generate the Blog.hbm.xml file.
精彩评论