一对多JPA关系和抽象类中的targetEntity



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

@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类。IItem接口。以及其他抽象类,它们扩展了AbstractItemJPA。

这是一个例子:

@MappedSuperclass
public abstract class AbstractControlItemJPA extends AbstractItemJPA implements IControlItem

看来EclipseLink不希望targetEntity参数使用AbstractClass。

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

有解决方案吗?

谢谢大家!

您没有向我们提供定义AbstractItemJPA的方法。

然而,用@MappedSuperclass注释的类不是实体。它只是一个用于与其子级共享映射信息的类。

因此,您无法创建与此类的关联。只能创建与实体的关联。

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

最新更新