我认为hibernate.cfg.xml和hibernate.properties是有效的等效,因此可以互换使用。这是真的吗?
如果是真的,那么hibernate.properties中的等效属性名称是什么对于hibernate.cfg.xml中有时出现的"mapping"标记?
例如,这里有一个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
但是,如何将其他标记(如"mapping")转换为hibernate.properties呢?
"我认为hibernate.cfg.xml和hibernate.properties实际上是等效的,因此可以互换使用。这是真的吗?"
来自文档
配置的另一种方法是指定名为hibernate.cfg.xml的文件中的配置。可以使用此文件作为hibernate.properties文件的替换,或者,如果两者都是存在,以覆盖属性。
"映射"标签
类到表的映射在hibernate映射标记中。在你的情况下,我认为这应该出现在Item.hbm.xml 中
可以将hibernate.cfg.xml转换为等效的hibernate属性文件。
如果不想使用mapping标记,可以使用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库。
请参阅层次映射