如何用一个以上的序列映射JPA中的继承? [英] How to map inheritence in JPA with more that one sequence?
问题描述
我有一个抽象类A,许多其他类都继承了它.
在我们的数据库(Oracle)中,这些类由每个类的一个表映射,并且继承由子表中的FK(子K上的FK引用A的PK)实现.
到目前为止,一切都很好.很标准.
I have an abstract class A that many other classes inherits it.
In our DB (Oracle), these classes are mapped by a table per class, and the inheritance is implemented by an FK in the child table (FK on the child's PK referencing A's PK).
So far so good. Pretty standard.
现在,我们如何生成PK?当然有一个序列...但是我们的数据库管理员决定,在继承A的特定表B中创建行时,PK将从另一个序列(B的序列)生成.
Now, how do we generate the PK? with a sequence of course... but our DBA decided that when creating rows in a specific table B which inherits A, the PK will be generated from a different sequence (B's sequence).
我的问题-如何在JPA中对此进行映射?我希望类B继承类A,但是在实例化该类时,我希望它的ID(即A的ID)使用不同的序列生成.
My question - how to map this in JPA? I want class B to inherit class A, but when it's instantiated I want its ID (which is A's ID) to be generated using a different sequence.
推荐答案
JPA没有提供一种方法来覆盖子类中的序列生成器.
JPA does not provide a way to override the sequence generator in a subclass.
您可以将超类设为@MappedSuperclass,而不在其中定义ID,然后可以在子类中对其进行不同的定义.
You could make the superclass a @MappedSuperclass, and not define the id in it, then you could define it differently in the subclass.
如果使用EclipseLink,则可以在子类上定义DescriptorCustomizer(@Customizer)并将描述符上的sequenceName设置为其他序列.
If using EclipseLink, you could define a DescriptorCustomizer (@Customizer) on the subclass and set the sequenceName on the descriptor to the other sequence.
这篇关于如何用一个以上的序列映射JPA中的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!