2016-05-13 86 views
0

我目前正在使用JPA(EclipseLink),但由于OneToMany关系的targetEntity参数而卡住了。targetEntity在OneToMany JPA关系和抽象类

@Entity 
@Table(name = "INVENTORY") 
public class InventoryJPA implements IInventory { 

    /** 
    * Unique identifier of the inventory 
    */ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID") 
    private int id; 

    /** 
    * The list of resources avalible in the inventory. 
    */ 
    private transient Map<Resource, Integer> resources = null; 

    /** 
    * The list of items in the inventory. 
    */ 
    @OneToMany(targetEntity = AbstractItemJPA.class, 
      cascade = CascadeType.ALL) 
    private List<IItem> items = null; 

我有一个AbstractItemJPA类impl。一个IItem界面。 和其他扩展AbstractItemJPA的抽象类。

这是一个例子:

@MappedSuperclass 
public abstract class AbstractControlItemJPA extends AbstractItemJPA implements IControlItem 

看来的EclipseLink不想要一个抽象类的targetEntity参数。

@OneToMany(targetEntity = AbstractItemJPA.class, 
       cascade = CascadeType.ALL) 
     private List<IItem> items = null; 

有没有解决方案呢?

谢谢大家!

回答

0

您没有给我们提供您定义AbstractItemJPA的方式。

尽管如此,用@MappedSuperclass注解的类不是一个实体。它只是一个用于与孩子共享地图信息的类。

因此,您无法为此课程创建关联。只有与实体的关联才能创建。

你应该问问自己,你是否真的需要这样一个复杂的类层次结构。保持你的实体模型简单并且控制你的类的层次结构将如何存储在表中(一个表或多个表?)。