简体   繁体   English

OneToMany JPA关系和AbstractClass中的targetEntity

[英]targetEntity In OneToMany JPA Relation and AbstractClass

I'm currently using JPA (EclipseLink) but I'm stuck because of the targetEntity parameter for a OneToMany relation. 我目前正在使用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;

I have an AbstractItemJPA class which impl. 我有一个暗示的AbstractItemJPA类。 a IItem interface. IItem接口。 And others Abstract classes which extend AbstractItemJPA. 其他扩展AbstractItemJPA的Abstract类。

This is a example : 这是一个例子:

@MappedSuperclass
public abstract class AbstractControlItemJPA extends AbstractItemJPA implements IControlItem

It seems that EclipseLink don't want an AbstractClass for the targetEntity parameter. 看来EclipseLink不需要对TargetEntity参数使用AbstractClass。

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

Is there a solution for this ? 有解决方案吗?

Thanks for all ! 谢谢大家!

You didn't give us the way you defined your AbstractItemJPA. 您没有给我们定义AbstractItemJPA的方式。

Nevertheless, a class annotated with @MappedSuperclass is not an entity. 但是,用@MappedSuperclass注释的类不是实体。 It is only a class used to share mapping information with its children. 它只是一个用于与其子级共享映射信息的类。

Thus you can't create an association to this class. 因此,您无法创建与此类的关联。 Only an association to an entity can be created. 只能创建与实体的关联。

You should ask yourself if you really need such a complex hierarchy of classes. 您应该问自己是否真的需要如此复杂的类层次结构。 Keep your entities model simple and keep control on how the hierarchy of your classes will be stored in table (one table or several tables ?). 使您的实体模型简单,并控制类的层次结构如何存储在表中(一个表还是几个表?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM