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

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

问题描述

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

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

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

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

现在我需要另一个表来存储相同 User 类型的数据,我只需要将我的实体保存到该表中.

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

数据结构(类似这样):

table users_1_table{
  string id;
  string username;
}

table users_2_table{
  string id;
  string username;
}

处理这个:

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

结果我应该在 users_1_tableuser2 中有 user1users_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?

推荐答案

前言:

即使在 SO 上,这也是一个被广泛问到的问题,而且答案也广泛与 Subclass 或实际上 SuperClass 方法有关(例如 [1])

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])

实际答案:

在这些帖子上 [2], [3] 他们建议使用带有 EntityName 参数的 xml 映射.

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

因此,使用 xml 映射您不需要超类,只需将 EntityName 参数提供给两个相同的映射即可.

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

示例映射:

<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)

_session.Save("User2Object", user2)

帖子 2 &3 被用作此代码段的基础.官方来源 [4]

比赛后:

关于第一个问题的一个答案,实际上是这篇文章的链接 [5] 有不同的方法:

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

您对对象的第一个实例说再见,将数据克隆到新实例并使用不同的名称保存它.因此,没有违反 Hibernate 逻辑和每个人的内容:两个表中的数据相同,没有使用子类.

嗯,这种方法的实现或代码或可信度是这样的,我也没有测试过.

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

另一种情况:

在这篇文章中 [6] 还有一个人试图用更简单的方法来挑战超类方法,但同样,最可靠的答案表明不可能有另一种方法,官方的非 xml 方法是所述的子类方法.

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.

来源

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

[2] 映射两个相同的表(相同的架构...)到 Hibernate 中的相同实体

[3] 如何映射 2相同的表(相同的属性)到 1 个实体

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

[5] Hibernate 4:一个类映射两个表 - 如何在两个表上持久化一个对象?

[6] 存在于超过 1 个目录

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

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