Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate two tables per one entity

I have one entity - User. It is described by User.class.

Hibernate creates one table per entity, so when I call session.save(user), my data is always saved to this table.

Now I need another table for data of same User type, and I need to save my entity to that table only.

Data structure (something like this):

table users_1_table{
  string id;
  string username;
}

table users_2_table{
  string id;
  string username;
}

work with this:

session.save(user1,"users_1_table")
session.save(user2,"users_2_table")

and in result I should have user1 in users_1_table and user2 in users_2_table.

Due to system limitation I can not put these two objects in one table. (Even creating extra field is bad idea).

Can I do this without subclassing? Using programmaticaly hibernate configuration?

like image 335
msangel Avatar asked May 26 '13 00:05

msangel


People also ask

Can one entity have multiple tables?

Solution: Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

Which annotation is used to link two tables through a relation table?

The @JoinColumn annotation is used to specify the join/linking column with the main table. Here, the join column is employee_id and project_id is the inverse join column since Project is on the inverse side of the relationship.

How do you map entity to a table?

In Spring Data JPA we can map an entity to a specific table by using @Table annotation where we can specify schema and name. But Spring Data JDBC uses a NamingStrategy to map an entity to a table name by converting the entities class name.

What is many-to-many relationship in hibernate?

Many-to-Many mapping is usually implemented in database using a Join Table. For example we can have Cart and Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here.


2 Answers

Preface:

This is a widely asked question even on SO, and also widely the answers are related to Subclass or actually SuperClass approach (e.g. [1])

Actual answer:

On these posts [2], [3] they suggest to use a xml mapping with EntityName parameter.

So, mapping with xml you don't need superclass, just give the EntityName parameter to two identical mappings.

Example Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="DomainModel.User, DomainModel"
     table="User1Object" entity-name="User1Object">  
         <id name="_id" access="field" column="id">
             <generator class="assigned"/>
         </id>
        <property name= ...>
 </class>
 <class name="DomainModel.User, DomainModel"
     table="User2Object" entity-name="User2Object">
         <id name="_id" access="field" column="id">
            <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("User1Object", user1)

or

_session.Save("User2Object", user2)

Posts 2 & 3 were used as a basis for this snippet. Official source [4]

After match:

One answer on the first question which is actually link to this post [5] there is different approach:

You say bye to the first instance of the object, clone the data to fresh instance and persist that with different name. Thus, no violation on Hibernate logic and everybody content: same data at two tables and no sub-classes used.

Well, the implementation or code or credibility of that approach is so and so, I haven't tested it neither.

Another case:

In this post [6] there is another person trying to challenge the super class approach with something simpler, but again, the most credible answer states it is not possible another way around, the official non-xml approach is the said subclass approach.

Sources

[1] How to map one class to different tables using hibernate/jpa annotations

[2] Map Two Identical tables ( same schema...) to same entity in Hibernate

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

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

[5] Hibernate 4: One class mapping Two tables - How to persist one object on both tables?

[6] Hibernate Annotation for Entity existing in more than 1 catalog

like image 136
mico Avatar answered Oct 19 '22 21:10

mico


Also it works using a default entity and an alternative one:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="DomainModel.User, DomainModel"
     table="User1Object">  
         <id name="_id" access="field" column="id">
             <generator class="assigned"/>
         </id>
        <property name= ...>
 </class>
 <class name="DomainModel.User, DomainModel"
     table="User2Object" entity-name="User2Object">
         <id name="_id" access="field" column="id">
            <generator class="assigned"/>
         </id>
        <property name= ...>
</class>
</hibernate-mapping>

For the default one, you can use the method

_session.Save(user1)

and

_session.Save("User2Object", user2)

for the alternative one.

like image 43
LieT13 Avatar answered Oct 19 '22 21:10

LieT13