2016-05-13 84 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註解的類不是一個實體。它只是一個用於與孩子共享地圖信息的類。

因此,您無法爲此課程創建關聯。只有與實體的關聯才能創建。

你應該問問自己,你是否真的需要這樣一個複雜的類層次結構。保持你的實體模型簡單並且控制你的類的層次結構將如何存儲在表中(一個表或多個表?)。