问题描述
我目前坚持什么似乎是一个很简单的问题,但我似乎无法找到出路:
I'm currently stuck with what seems to be a very simple problem, but I just can't seem to find a way around:
我有2个相同表:
- tbl_creditcard_approved_txns
- tbl_creditcard_declined_txns
在这两个字段是相同的,我有一个类 - 交易
用于重新present表中的所有相应字段英寸
The fields in both are identical, and I have one class - Transaction
that is used to represent all the appropriate fields in the tables.
我试图映射两个不同的实体(每个表)上面的类。在旧世界,我已经创建了两个的hbm.xml
文件,一个用于每个表和他们都映射到交易
。那么我的持久过程中使用的实体名称,以确保对象获取正确的表依然存在,视情节。
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.xml
files, 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.
我试图使用注解目前达到相同的,但在2实体映射到单类到目前为止,有没有运气。这是可能的呢?
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 single class. Is this possible at all?
我目前使用中,我已经提取的所有公共字段不同的方法(相同的列名)到 @MappedSuperclass
,并创建了两个独立的类(为每个实体)从超类扩展(这些类只是有不同的列名,如适用)。
I'm currently using a different approach in that I've extracted all the common fields (identical column names) into an @MappedSuperClass
and 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,您可以进行如下操作:
Using @MappedSuperclass, you would proceed as follows:
@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 ...
使用@AttributeOverride覆盖两种交易之间的列名对象,如果需要的话。
Use @AttributeOverride to override column names between the two types of Transaction objects, if needed.
更新:我看到你想要一个@Entity两个表映射在相同的EntityManagerFactory ...我不认为你可以做到这一点。
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.
这篇关于如何使用Hibernate / JPA注解映射一个类到不同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!