持久子类的多个鉴别器列
我因一个非常特殊的问题而困扰自己。使用 OpenJPA (KODO 4.1) 有没有一种方法可以使用多个列作为鉴别器列?
我的问题是我有一个与此类似的表结构(当然我修改的能力有限):
Table VEHICLE EXPENSIVE_CAR CHEAP_CAR EXPENSIVE_BOAT CHEAP_BOAT
---------------------------------------------------------------------------------
HORSE_POWER LUXURY_ACC CLASIFICATION SIZE SIZE
MEDIUM EXTRAS TV_SIZE
IS_EXPENSIVE CLASIFICATION
介质会区分船和汽车并且昂贵会区分昂贵或便宜。
那么,有没有什么方法可以通过 OpenJPA 提供的继承功能来实现这一点(我知道 hibernate 可以使用鉴别器公式,但我试图不从默认的 JPA 提供程序切换)。
如果您能告诉我有关自定义 鉴别器策略 非常棒,因为我有预感它可能是一个合理的解决方案(尽管我更喜欢独立于供应商的解决方案)
非常感谢
I have banged my self with a very particular problem. Using OpenJPA (KODO 4.1) is there a way to use more than one column as a discriminator column?
My problem is that i have a table structure (which i have limited ability to modify of course) similar to this:
Table VEHICLE EXPENSIVE_CAR CHEAP_CAR EXPENSIVE_BOAT CHEAP_BOAT
---------------------------------------------------------------------------------
HORSE_POWER LUXURY_ACC CLASIFICATION SIZE SIZE
MEDIUM EXTRAS TV_SIZE
IS_EXPENSIVE CLASIFICATION
Where medium would discriminate between boat and car and is expensive would discriminate bettwen expensive or cheap.
So, is there any way to achieve this with the inheritance capabilities provided by OpenJPA (i know hibernate can use discriminator formulas but i am trying not to switch from the default JPA provider).
As a bonus if you can tell me about the custom discriminator strategies from OpenJPA that would be great since i have a hunch that it could be a plausible solution (even though i would prefer a vendor independent one)
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们从后面开始。
鉴别器策略定义区分层次结构中相关实体的列的类型。在 JPA 1.x 中,它可以是字符串(这是默认值)、字符和整数。
示例:
如果您使用单表继承策略作为默认策略,则此设置意味着层次结构中的所有这些实体都将映射到父类的数据库表,这意味着您的数据库中将有一个 FOO 表类 Foo、Bar 和 Baz 的所有属性加上一个名为 TYPE 的鉴别器列,其类型为字符串(很可能是一些 varchar 变体,长度为 5),并且对于每个实体类型,在持久化时将自动插入相应的鉴别器值。
当您使用 JPQL 查找 Bar 或 Baz 实体时,JPA 将能够从 FOO 表中查找实体(因为这是父实体的表),并且通过依赖鉴别器列的内容,您的 JPA 提供程序将能够区分创建 Bar 或 Baz 实体。
如果将鉴别器类型设置为 INTEGER 或 CHAR,则可以分别写入值 1、2、3 或“A”、“B”、“C”等。
现在回答 OpenJPA 问题。
AFAIK 无法使用 OpenJPA 轻松指定多个鉴别器值,但您可以创建一些更复杂的实体层次结构,因此如果您能够修改架构,您可以创建一个车辆实体、一辆汽车、一艘船以及昂贵的船和昂贵的汽车。
如果您必须坚持您的架构,我猜(但请修复)您正在使用连接或每类表继承策略,这意味着您无法使用 JPA 提供的鉴别器功能。
Let's start backwards.
Discriminator strategies define the type of the column that discriminates related entities in the hierarchy. In JPA 1.x, it can be either a string (this is the default), a char and an integer.
Example:
If you are using is single table inheritance strategy as the default, this setup means that all these entities that are in the hierarchy will be mapped to the database table of the parent class, meaning you'll have a FOO table in your db with all the attributes of classes Foo, Bar and Baz plus a discriminator column called TYPE with the type of string (most likely some varchar-variant, length 5) and for each entity type, the respective discriminator value will be inserted automatically when persisted.
When you are finding Bar or Baz entities with JPQL, JPA will be able to find the entities from the FOO table (because that is the parent entity's table), and by relying on the contents of the discriminator column, your JPA provider will be able to discriminate between creating some Bar or Baz entities.
If you'd set the discriminator type to INTEGER or CHAR, then you could write for the values 1, 2, 3 or "A", "B", "C" etc. respectively.
Now to the OpenJPA question.
AFAIK there's no way to easily specify multiple discriminator values with OpenJPA, but you can create some more complex entity hierarchies, so if you'd be able to modify the schema, you could create a Vehicle entity, a Car, a Boat and both an ExpensiveBoat and an ExpensiveCar.
If you have to stick with your schema, I guess (but FIXME) you are using the joined or the table per class inheritance strategy which means you cannot use the discriminator feature that JPA provides.