4

I have table1 and table2 that have the same schema... and I want to have only one entity rather than two ( since the fields will be the same). How would I achieve that in hibernate with XML mapping. So my goal is when I m querying in the DAO, how would it know which table to pull from if both tables are mapped to the same entity.

I m trying to Not create a parent class and then two subclasses.

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sammy
  • 4,538
  • 8
  • 33
  • 52
  • 1
    More importantly, which table would it write to? – skaffman Feb 14 '11 at 22:22
  • correct, I want to be able to read and write to two tables, use two mapping files and only one class. not sure if it is even possible. but said let me ask the experts in StackOverflow and see if it is possible to specify the table at the DAO level or something similar. – Sammy Feb 14 '11 at 22:29
  • 1
    see also http://stackoverflow.com/questions/3505866/how-to-map-one-class-to-different-tables-using-hibernate-jpa-annotations. – Péter Török Feb 14 '11 at 22:34
  • Thanks for the reference. I looked at those questions before. but they all use inheritence. I m trying to not use inheritence and no annotation. – Sammy Feb 14 '11 at 22:37

1 Answers1

6

Sorry for the late answer. i have answered this question several times on stackoverflow.


To map two identical tables onto one entity 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 (Order and OrderHistory) 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 the type of entity you need, you simply call the appropriate Session methods as:

_session.Save("Order", myOrder) 

or

_session.Save("OrderHistory", myOrder)

Simple, isn't ?

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

Chucky
  • 545
  • 7
  • 14
Sisyphus
  • 4,181
  • 1
  • 22
  • 15
  • 1
    What if I don't call the save method and entities are persisted on transaction commit ? I mean that I would like to persist to different tables depending to which parent entity the order class belongs. – Tomasz Jaskuλa Sep 29 '12 at 15:12
  • Is there a way to do this with annotations? – LeHill Dec 30 '15 at 21:49
  • Found that answer: http://stackoverflow.com/questions/3505866/how-to-map-one-class-to-different-tables-using-hibernate-jpa-annotations – LeHill Dec 31 '15 at 16:55