每个实体休眠两个表 [英] hibernate two tables per one entity

查看:141
本文介绍了每个实体休眠两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体 - User 。它由 User.class



描述。Hibernate为每个实体创建一个表,所以当我调用 session.save(user),我的数据总是保存到这个表中。



现在我需要另一张表 User type,我需要将实体保存到该表中。



数据结构< (例如这样):

 表users_1_table {
字符串id;
字符串用户名;
}

表users_2_table {
字符串ID;
字符串用户名;
}

使用此功能

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

结果我应该在 users_1_table中包含 user1 user2 位于 users_2_table 中。



由于系统限制,我不能将这两个对象放在一个表中。 (即使创建额外的领域也是不好的主意)。



我可以在没有子类化的情况下执行此操作吗?使用programmaticaly hibernate配置?

解决方案

前言:

即使是在SO上,这也是一个广泛的问题,并且广泛的答案也与子类或实际上 SuperClass 1 ])

实际答案:

a href =https://stackoverflow.com/questions/4997950/map-two-identical-tables-same-schema-to-same-entity-in-hibernate> 2 ],[ 3 ]他们建议使用xml映射 EntityName 参数。

所以,用xml映射你不需要supeclass,只需给 EntityName 参数映射到两个完全相同的映射。



示例映射

 < hibernate-mapping xmlns =urn:nhibernate-mapping -2.2\" > 
< class name =DomainModel.User,DomainModel
table =User1Objectentity-name =User1Object>
< id name =_ idaccess =fieldcolumn =id>
< generator class =assigned/>
< / id>
< property name = ...>
< / class>
< class name =DomainModel.User,DomainModel
table =User2Objectentity-name =User2Object>
< id name =_ idaccess =fieldcolumn =id>
< generator class =assigned/>
< / id>
< property name = ...>
< / class>
< / hibernate-mapping>

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


$ b

_session.Save(User1Object,user1)




$ b _session.Save(User2Object,user2)



帖子2& 3被用作此片段的基础。官方消息来源[ 4 ]

问题其实是链接到这篇文章[ 5 ]有不同的方法:

你说对象的第一个实例,克隆数据到新鲜的实例并坚持不同的名称。因此,没有违反Hibernate逻辑和每个人的内容:在两个表中使用相同的数据并且没有使用子类。



那么,实现,代码或可信度这种做法是如此,我也没有对它进行过测试。



另一种情况:

在这篇文章中[ 6 ]还有一个人试图用更简单的方法来挑战超类方法,但最可信的答案表明它不可能有其他方法,正式的非xml方法就是所说的子类方法。

消息来源 $ b

[1] 如何使用hibernate / jpa注释将一个类映射到不同的表



[2]

/ a>



[3] http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html#mapping-entityname =nofollow noreferrer> http://docs.jboss.org/hibernate/ core / 3.2 / reference / en / html / mapping.html#mapping-entityname
$ b

[5]



[6] 超过1个目录中的实体的Hibernate Annotation


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?

解决方案

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 supeclass, 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

这篇关于每个实体休眠两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆