【问题标题】:Does the "mapping" tag in hibernate.cfg.xml have an equivalent property name in hibernate.properties?hibernate.cfg.xml 中的“映射”标签在 hibernate.properties 中是否具有等效的属性名称?
【发布时间】:2013-07-11 07:48:32
【问题描述】:

我认为 hibernate.cfg.xml 和 hibernate.properties 是有效的 等价的,因此可以互换使用。这是真的吗?

如果为真,那么 hibernate.properties 中的等价属性名是什么 对于有时出现在 hibernate.cfg.xml 中的“映射”标签?

例如,这里是一个示例 hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory
        name="java:hibernate/SessionFactory">

        <!-- properties -->
        <property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">false</property>
        <property name="transaction.factory_class">
            org.hibernate.transaction.JTATransactionFactory
        </property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>

        <!-- mapping files -->
        <mapping resource="org/hibernate/auction/Item.hbm.xml"/>
        <mapping resource="org/hibernate/auction/Bid.hbm.xml"/>

        <!-- cache settings -->
        <class-cache class="org.hibernate.auction.Item" usage="read-write"/>
        <class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
        <collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
    </session-factory>
</hibernate-configuration>

我知道如何将这个 hibernate.cfg.xml 中的一些(但不是全部)标签转换为 hibernate.properties:

hibernate.connection.datasource=java:/comp/env/jdbc/MyDB
hibernate.dialect=org.hibernate.dialect.MySQLDialect

但是如何将其他标签(例如“映射”)转换为 hibernate.properties?

【问题讨论】:

    标签: spring hibernate jakarta-ee hibernate-mapping


    【解决方案1】:

    "我以为 hibernate.cfg.xml 和 hibernate.properties 是 等效,因此可以互换使用。 这是真的吗?”

    来自文档

    另一种配置方法是指定一个完整的 在名为 hibernate.cfg.xml 的文件中进行配置。这个文件可以用 作为 hibernate.properties 文件的替代品,或者,如果两者都是 存在,以覆盖属性。

    “映射”标签

    类到表的映射在 hibernate-mapping 标签内。在您的情况下,我认为这应该存在于 Item.hbm.xml

    是的 hibernate.cfg.xml 可以翻译成等效的休眠属性文件。

    如果你不想使用映射标签,你可以使用class元素声明一个持久类。

    <hibernate-configuration>
          <session-factory>
            <mapping class="com.mycompany.sample.domain.Order"/>
            <mapping class="com.mycompany.sample.domain.LineItem"/>
          </session-factory>
        </hibernate-configuration>
    

    你可以使用Hibernate Annotations library

    参考hibernte-mapping

    【讨论】:

    • hibernate 规范可能会说明如何解决同时定义了 hibernate.cfg.xml 和 hibernate.properties 的情况。然而,这并不是我要问的。我的问题是:hibernate.cfg.xml 可以翻译成等效的 hibernate.properties 文件吗?
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2011-11-08
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多