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

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

问题描述

我创建JPA实体存储有关客户的信息。我有一个客户叫抽象类。它有两个被称为购物者和用户的子类。无论购物者和用户都在我创建称为元数据存储另一类键值对的形式对他们的元数据。我的问题是,如何有序列的元数据通过抽象类客户补充的吗?下面是一些code,所以你可以看到我在说什么:

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
}

下面是元数据类 -

Here is the metaData class-

@Entity
public class MetaData implements Serializable
{

@EmbeddedId
protected MetaDataId id;

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

如果我只有一个孩子上课说的用户,这工作正常和元数据表得到一个名为metaData_order列,一切都很好。问题是,当我添加了购物的实体,现在元数据表试图插入两个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))

如果我补充一点,实现客户第三个孩子上课,语句试图插入三个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.

如果你的情况,你也许可以重复使用相同的顺序列,如相同的元数据不应该有一个以上的所有者。这似乎是在的EclipseLink的TABLE_PER_CLASS支持一个bug,尝试最新发布/版本,如果仍然失败,请记录错误,并投赞成票。您也可以尝试不同类型的产业,如加盟,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天全站免登陆