How do I make an abstract JPA entity that has an ordered column in a shared object?

社会主义新天地 提交于 2019-12-11 20:11:36

问题


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;
 .... 
}

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))

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/18215116/how-do-i-make-an-abstract-jpa-entity-that-has-an-ordered-column-in-a-shared-obje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!