Jpa 映射的超类和多对多不能一起工作

Jpa 映射的超类和多对多不能一起工作,jpa,spring-data-jpa,Jpa,Spring Data Jpa,嗨,我的数据模型是这样的:项1和项2具有相同的属性,我将其放入类AbstractItem中。现在我有了一个容器类,它包含类型为item1或item2的多个项。我的源代码如下所示 @MappedSuperclass public class AbstractItem { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name;

嗨,我的数据模型是这样的:项1和项2具有相同的属性,我将其放入类AbstractItem中。现在我有了一个容器类,它包含类型为item1或item2的多个项。我的源代码如下所示

@MappedSuperclass
public class AbstractItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    public AbstractItem() {
    }
}
@Entity
public class Item1 extends AbstractItem{
    public Item1() {
    }
}

@Entity
public class Item2 extends AbstractItem {
    public Item2() {
        super();
    }

}

@Entity
public class Container {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToMany
    private List<AbstractItem> abstractItemList;
}

问题在于
AbstractItem
不是一个实体,而是一个
MappedSuperclass

因此,您不能在
@manytomy
中使用
AbstractItem

使用其中一种类型,或者如果要使用继承,则必须使用
@heritation
而不是
@MappedSuperclass

@Entity
@Inheritance
public class AbstractItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    public AbstractItem() {
    }
}
默认的继承策略是SINGLE_TABLE,因此继承层次结构的所有实体都将存储在同一个表中。
还有其他选择。看看

问题在于,
AbstractItem
不是一个实体,而是一个
MappedSuperclass

因此,您不能在
@manytomy
中使用
AbstractItem

使用其中一种类型,或者如果要使用继承,则必须使用
@heritation
而不是
@MappedSuperclass

@Entity
@Inheritance
public class AbstractItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    public AbstractItem() {
    }
}
默认的继承策略是SINGLE_TABLE,因此继承层次结构的所有实体都将存储在同一个表中。 还有其他选择。看看