首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hbm2hbmxml不生成hibernate映射。

hbm2hbmxml不生成hibernate映射。
EN

Stack Overflow用户
提问于 2011-07-29 09:34:20
回答 2查看 949关注 0票数 0

我在每个地方都搜索这个,但是找不到引用的ANT,并使用作为教程编写一个ANT任务,将带有EJB3注释的POJO转换为hibernate映射文件(hmb.xml)。

我的构建在没有错误的情况下成功结束,但是我在目标目录或任何地方找不到任何hbm.xmk。

这是我使用的代码。

build.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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;
  }
}

输出

代码语言:javascript
复制
    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文件是

代码语言:javascript
复制
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

请帮助我,这个任务没有多少教程或工作示例示例代码,特别是用于使用注释s配置从POJO生成hbm文件的示例代码。

EN

回答 2

Stack Overflow用户

发布于 2011-07-29 18:22:52

为什么要将注释转换为XML配置?许多EJB3注释具有直接的Hibernate等价物,或者实际上使用相同的注释(javax.persistence.*)。我只想设置您的项目,以编译针对Hibernate,看看什么会爆炸,修复它们,并继续前进。那么您就不需要使用XML文件了。

票数 0
EN

Stack Overflow用户

发布于 2011-07-30 17:09:04

好的,我找到了原因:我没有在hibernate.cfg.xml文件中添加到类的映射

以前是

代码语言:javascript
复制
<?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>

但它应该是

代码语言:javascript
复制
<?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文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6871177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档