【问题标题】:Hibernate hbm2ddl ant file paths休眠 hbm2ddl ant 文件路径
【发布时间】:2011-04-13 03:10:13
【问题描述】:

我在使用 Hibernate Tools 生成数据库架构时遇到问题。这是我的 ant 文件

<project name="Schema generator for MySQL database" basedir=".">
   <description>
 This file is used for running Hibernate Tools Ant task.
    It is used to generate database schema based on hibernate configuration
   </description>

   <path id="toolslib">
      <path location="lib/hibernate-tools.jar" />
      <path location="lib/hibernate-3.2.4.ga.jar" />
      <path location="lib/freemarker.jar" />
      <path location="lib/mysql-connector-java-5.1.13" />
      <path location="lib/dom4j-1.6.1.jar" />
      <path location="hibernate_mappings/Address.hbm.xml" />
   </path>

   <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />

   <hibernatetool destdir="${build.dir}/generated">
      <classpath>
         <path location="${build.dir}/classes" />
      </classpath>
      <configuration configurationfile="hibernate.cfg.xml" />
      <hbm2ddl />
      <hbm2dao />
   </hibernatetool>
</project>

运行 ant 任务时出现此错误:

C:\work\gwt_workspace\billing-cms\dao\src\main\resources\build.xml:19: org.hibernate.MappingNotFoundException: 资源: hibernate_mappings/Address.hbm.xml 未找到

我的文件系统路径层次结构是这样的:

+resources
   -hibernate_mappings
      -Address.hbm.xml
      -User.hbm.xml
      -etc..
   -hibernate.cfg.xml
   -build.xml

我在 hibernate.hbm.xml 中定义了我的映射,如下所示:

<mapping resource="hibernate_mappings/Address.hbm.xml" />

【问题讨论】:

    标签: hibernate ant hbm2ddl


    【解决方案1】:

    您不需要 Ant 来执行此操作。如果您只运行一个 Hibernate 应用程序(例如,一个虚拟测试或主程序),它会为您运行 hbm2ddl。

    确保运行时 .hbm.xml 文件位于 Ant 类路径中。也许这就是问题所在。 (没有仔细查看你的 build.xml 就知道了;只是把它扔在那里。)

    这是一个有效的示例 Hibernate 配置,包括 hbm2ddl。在此之后为您的图案设计:

    <!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="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?autoReconnect=true</property>
            <property name="connection.username">hibernate</property>
            <property name="connection.password">hibernate</property>
            <property name="connection.pool_size">1</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            <property name="show_sql">true</property>
            <property name="generate_statistics">true</property>
            <property name="query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
            <property name="cache.use_minimal_puts">false</property>
            <property name="cache.use_query_cache">false</property>
            <property name="order_updates">false</property>
            <property name="hbm2ddl.auto">create-drop</property>
            <property name="current_session_context_class">thread</property>
    
            <mapping resource="hibernate/policy/persistence/hibernate/Person.hbm.xml"/>
    
        </session-factory>
    </hibernate-configuration>
    

    【讨论】:

    • 好吧,我没有提供我的整个 hibernate.cfg.xml。我总是需要 hbm2ddl.auto 属性吗?
    • 不,如果你不需要它。我通常让它从对象创建架构,然后将其注释掉。
    猜你喜欢
    • 2023-04-06
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2013-12-06
    • 2013-02-15
    相关资源
    最近更新 更多