2

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.

Nathanphan
  • 947
  • 2
  • 11
  • 23

1 Answers1

3

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.

Sisyphus
  • 4,181
  • 1
  • 22
  • 15
  • thank Sisyphus. However, I am using hibernate annotation to map, so can you me out what should I do with annotation to follow your solution? – Nathanphan Mar 08 '11 at 02:04
  • NO i cannot help with that at all. entity-name is largely overlooked and not even well documented for regular Hibernate. I could not determine if it is even supported for Annotations. You should specify in your question that you are using annotations, my impression is that most people are not using them. – Sisyphus Mar 08 '11 at 06:36
  • 1
    @Nathanphan This feature is not supported in Annotations: [http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/mapping.html#mapping-entityname](http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/mapping.html#mapping-entityname) – Carlos Nov 20 '12 at 11:26
  • Getting 'org.hibernate.MappingException: Unknown entity: xxx' error after adding 'table' and 'entity-name' tags in the xml mapping file. I am trying to map two different tables to the same java class here. It is working fine with two different xml mapping files. Trying to combine it with the above mentioned method. Any Idea why this error occurs? Anything that I am missing? – Aparna Jan 18 '17 at 08:39