如何创建在共享对象中具有有序列的抽象 JPA 实体? [英] How do I make an abstract JPA entity that has an ordered column in a shared object?

查看:21
本文介绍了如何创建在共享对象中具有有序列的抽象 JPA 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建 JPA 实体来存储有关客户的信息.我有一个叫做客户的抽象类.它有两个子类,称为购物者和用户.购物者和用户都以键值对的形式拥有关于他们的元数据,我创建了另一个名为 MetaData 的类来存储这些元数据.我的问题是,如何通过客户的抽象类向元数据添加有序列?这是一些代码,所以你可以看到我在说什么:

I am creating JPA entities to store information about customers. I have an abstract class called customer. It has two child classes called shoppers and users. Both shoppers and users have metadata about them in the form of key value pairs which I have created another class called MetaData to store. My question is, how do I add an ordered column to metadata via the abstract class of customers? Here is some code so you can see what I am saying:

@Inheritance( strategy = TABLE_PER_CLASS )
public abstract class Customer implements Serializable
{

@OneToMany( mappedBy = "parent", orphanRemoval = false  )
@OrderColumn //THIS IS CAUSING AN ERROR, BUT I WANT AN ORDERED COLUMN - PLEASE HELP
private List<MetaDataType> metaData;
}

用户和购物者类本质上是相同的,这里没有什么特别的 -

The user and shopper class are the same essentially, nothing special here -

@Entity
public class User extends Customer implements Serializable
{
  ...some user specific stuff
}

这里是元数据类-

@Entity
public class MetaData implements Serializable
{

@EmbeddedId
protected MetaDataId id;

@ManyToOne
@JoinColumn( name = "parentGuid", referencedColumnName = "guid", insertable = false,  updatable = false   )
protected Customer parent;
 .... 
}

如果我只有一个子类说 User,这很好用,并且元数据表有一个名为 metaData_order 的列,一切都很好.问题是当我添加 Shopper 实体时,现在元数据表尝试插入两个 MetaData_order 列并抛出此异常 -

If I just have one child class say User, this works fine and the metaData table gets a column called metaData_order and all is well. The problem is when I add the Shopper entity, now the metaData table tries to insert two MetaData_order columns and throws this exception -

java.sql.SQLSyntaxErrorException: Column name 'METADATA_ORDER' appears more than once in the CREATE TABLE statement.

Call: CREATE TABLE METADATA (VALUE VARCHAR(255), parentGuid VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL, metaData_ORDER INTEGER, metaData_ORDER INTEGER, PRIMARY
KEY (parentGuid, name))

如果我添加实现 Customer 的第三个子类,该语句会尝试插入三个 metaData_ORDER 列.显然我没有正确地进行这种抽象,我错过了什么?

If I add a third child class that implements Customer, the statement tries to insert three metaData_ORDER columns. Obviously I am not doing this abstraction correctly, what am I missing?

推荐答案

如果目标对象是共享的,那么就需要使用@JoinTable来存储关系和OrderColumn.

If the target object is shared, then you need to use a @JoinTable to store the relationship and the OrderColumn.

如果是您的情况,您可能可以重复使用相同的订单列,因为相同的 MetaData 永远不应该有多个所有者.这似乎是 EclipseLink 的 TABLE_PER_CLASS 支持中的错误,请尝试最新版本/构建,如果仍然失败,请记录错误并投票支持.您也可以尝试不同类型的继承,例如 JOINED,TABLE_PER_CLASS 通常不是一个好的解决方案.您也可以使用自己的脚本自行创建表格.

If your case, you could probably reuse the same order column, as the same MetaData should never have more than one owner. This seems to be a bug in EclipseLink's TABLE_PER_CLASS support, try the latest release/build, and if still fails please log a bug and vote for it. You could also try a different type of inheritance, such as JOINED, TABLE_PER_CLASS is not normally a good solution. You could also create the table yourself with your own script.

这篇关于如何创建在共享对象中具有有序列的抽象 JPA 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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