2011-07-29 42 views
0

我有搜索此的每一個地方,但不可能找到的answere IM以下this參考和使用this作爲教程寫一個Ant任務的POJO 3所標註的轉換與EJB到Hibernate映射文件(hmb.xml)hbm2hbmxml不會產生Hibernate映射

我的構建成功結束沒有錯誤,但我不能發現其中

這裏是我使用的代碼的目標目錄或任何內部的任何hbm.xmk。

的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; 

    @Column(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 

和JAR文件我使用的

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 

請幫我沒有太多的教程或工作示例的範例代碼,這個任務特意從POJO S使用annotationsconfiguration產生HBM文件

回答

0

爲什麼你想從註釋轉換爲XML配置?許多EJB3註釋具有直接的Hibernate等價物,或者實際上使用相同的註釋(javax.persistence.*)。我只是設置你的項目來對付Hibernate進行編譯,看看會發生什麼,修復它們,然後繼續。那麼你不需要XML文件。

0

好吧,我已經找到了原因:d 我沒有添加映射到類的hibernate.cfg.xml文件

以前是

<?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> 

但應

<?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> 

缺少的是「」部分現在它生成Blog.hbm.xml文件。