How to map 2 identical tables (same properties) to 1 entity

后端 未结 1 1571
深忆病人
深忆病人 2020-12-04 00:25

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 t

相关标签:
1条回答
  • 2020-12-04 01:10

    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.

    0 讨论(0)
提交回复
热议问题