本文介绍了如何将2个相同的表(相同的属性)映射到1个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个问题。

我有两个相同的表,一个是时间表,另一个是timetable_bk。 2个表具有相似的属性或字段。现在我想映射2个表到一个实体类(alltimetable)。

我已经尝试了Inheritance.TABLE_PER_CLASS策略,但是当我使用AllTimetable进行查询时。它什么都不会返回。



请帮助我。我搜索了很多次,但没有得到答案。



最好的问候。

解决方案

为了将两个相同的表映射到一个类,您需要使用Hibernate或NHibernate的entity-name属性。文档在这里:





例如,要将单个类Order映射到Order和OrderHistory表,您可以创建一个映射文件,该映射文件使用新实体名称将订单类映射到两个表中:

 < hibernate-mapping xmlns =urn:nhibernate-mapping-2.2> 
< class name =DomainModel.Order,DomainModel
table =Ordersentity-name =Order>`
< id name =_ idaccess =字段column =OrderId>
< generator class =assigned/>
< / id>
< property name = ...>
< / class>
< class name =DomainModel.Order,DomainModel
table =OrderHistoriesentity-name =OrderHistory>
< id name =_ idaccess =fieldcolumn =OrderId>
< generator class =assigned/>
< / id>
< property name = ...>
< / class>
< / hibernate-mapping>

然后根据您需要的实体类型,调用相应的会话方法:

  _session.Save(Order,myOrder)

  _session.Save(OrderHistory,myOrder)
code>

等。通常,实体名称必须在所有Hibernate调用中替换类名称。


Please help me on this problem.

I have 2 identical tables, one is timetable and another is timetable_bk. the 2 tables have similar properties or fields. Now I want to map 2 tables to one entity class (alltimetable).

I already tried Inheritance.TABLE_PER_CLASS strategy, but when I query using from AllTimetable. it return nothing.

Please help me. I search many times but did not get the answer yet.

Best Regards.

解决方案

To map two identical tables to one class you need to use the entity-name property of Hibernate or NHibernate. Documentation is here:

http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname

For example, to map a single class Order to Order and OrderHistory tables you create a mapping file that maps the order class to the two tables using new entity-names like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 <class name="DomainModel.Order, DomainModel"
     table="Orders" entity-name="Order">`  
         <id name="_id" access="field" column="OrderId">
             <generator class="assigned"/>
         </id>
        <property name= ...>
 </class>
 <class name="DomainModel.Order, DomainModel"
     table="OrderHistories" entity-name="OrderHistory">
         <id name="_id" access="field" column="OrderId">
            <generator class="assigned"/>
         </id>
        <property name= ...>
</class>
</hibernate-mapping>

Then depending on which type of entity you need you call the appropriate session methods as:

_session.Save("Order", myOrder) 

or

_session.Save("OrderHistory", myOrder)

etc. In general entity-name must replace class name in all Hibernate calls.

这篇关于如何将2个相同的表(相同的属性)映射到1个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:05