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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3505866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:23:29  来源:igfitidea点击:

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

javahibernateormjpaannotations

提问by Jay

I'm currently stuck with what seems to be a very simple problem, but I just can't seem to find a way around:

我目前被困在一个似乎非常简单的问题上,但我似乎无法找到解决方法:

I have 2 identicaltables:

我有 2 个相同的表:

  1. tbl_creditcard_approved_txns
  2. tbl_creditcard_declined_txns
  1. tbl_creditcard_approved_txns
  2. tbl_creditcard_declined_txns

The fields in both are identical, and I have one class - Transactionthat is used to represent all the appropriate fields in the tables.

两者中的字段是相同的,我有一个类 -Transaction用于表示表中的所有适当字段。

I'm trying to map two different entities (one for each table) to the above class. In the old world, I'd have created two hbm.xmlfiles, one for each table and map both of them to Transaction. I'd then use the entity name during persistence to ensure that the object gets persisted in the correct table, depending on the circumstance.

我正在尝试将两个不同的实体(每个表一个)映射到上述类。在旧世界中,我会创建两个hbm.xml文件,每个表一个,并将它们都映射到Transaction. 然后我会在持久化期间使用实体名称来确保对象在正确的表中持久化,具体取决于具体情况。

I am trying to use annotations currently to achieve the same but have had no luck so far in mapping the 2 entities to a singleclass. Is this possible at all?

我目前正在尝试使用注释来实现相同的目的,但到目前为止在将 2 个实体映射到单个类方面没有运气。这可能吗?

I'm currently using a different approach in that I've extracted all the common fields (identical column names) into an @MappedSuperClassand have created two separate classes (one for each entity) that extend from the super class (these classes just have the same fields with different column names, where applicable).

我目前正在使用一种不同的方法,因为我已经将所有公共字段(相同的列名)提取到一个中@MappedSuperClass,并创建了从超类扩展的两个单独的类(每个实体一个)(这些类只是具有相同的具有不同列名的字段(如适用)。

回答by les2

Using @MappedSuperclass, you would proceed as follows:

使用@MappedSuperclass,您将按如下方式进行:

@MappedSuperclass
public class Transaction ...

@Entity
@Table(name="tbl_creditcard_approved_txns")
public class DeclinedTransaction extends Transaction ...

@Entity
@Table(name="tbl_creditcard_declined_txns")
public class ApprovedTransaction extends Transaction ...

Use @AttributeOverride to override column names between the two types of Transaction objects, if needed.

如果需要,使用 @AttributeOverride 覆盖两种类型的 Transaction 对象之间的列名称。

Update: I see that you want to map one @Entity to two tables in the same EntityManagerFactory ... I don't think you can do that.

更新:我看到你想将一个 @Entity 映射到同一个 EntityManagerFactory 中的两个表......我认为你不能这样做。

回答by Jacob

The other way to do it would be to use a partioned table on the database layer that would then make visible one single table to your java code.

另一种方法是在数据库层上使用一个分区表,然后使一个表对您的 Java 代码可见。

This is probably the way to go as a general rule, the more smart partitioning you do, the faster your queries can be.

这可能是一般规则,您进行的分区越智能,您的查询速度就越快。

回答by Javid Jamae

You have to use two different persistence units or two separate entities. This was already answered here.

您必须使用两个不同的持久性单元或两个独立的实体。这已经在这里回答