Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据Jpa继承:保留实体Id';儿童实体中的s_Java_Spring Boot_Spring Data Jpa_Spring Data - Fatal编程技术网

Java Spring数据Jpa继承:保留实体Id';儿童实体中的s

Java Spring数据Jpa继承:保留实体Id';儿童实体中的s,java,spring-boot,spring-data-jpa,spring-data,Java,Spring Boot,Spring Data Jpa,Spring Data,我正在处理两个具有树状结构的实体,它们变得越来越复杂,因此我决定为它们创建一个抽象类,这样代码就更易于维护: @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class TreeStructure<T extends TreeStructure> { @ManyToOne protected T parent; @OneToMany(mappe

我正在处理两个具有树状结构的实体,它们变得越来越复杂,因此我决定为它们创建一个抽象类,这样代码就更易于维护:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TreeStructure<T extends TreeStructure>
{
    @ManyToOne
    protected  T parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    protected Set<T> children = new HashSet<>();
    //...
@实体
@继承(策略=继承类型。每个类的表)
公共抽象类树结构
{
@许多酮
受保护的T型双亲;
@OneToMany(mappedBy=“parent”,fetch=FetchType.LAZY)
protected Set children=new HashSet();
//...
然后我有两个实体扩展它:

@Entity(name = "TreeStructureOne")
public class TreeStructureOne extends TreeStructure<TreeStructureOne>
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty("TreeStructureOne_id")
    private long id;
@Entity(name=“treeststructureone”)
公共类树结构One扩展了树结构
{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@JsonProperty(“TreeStructureOne_id”)
私人长id;
我基本上希望数据库完全不知道这个
treeststructure
抽象,保存每个实体表中的所有字段,并期望
InheritanceType.TABLE\u PER\u CLASS
来处理这个问题。但似乎我至少需要在
treeststructure
实体中定义
Id
,否则我会得到:

调用init方法失败;嵌套异常为org.hibernate.AnnotationException:未为实体指定标识符:treeststructure

我不想在抽象类中添加一个
ID
,因为这会在数据库中创建三个表:
HT\u TREE\u STRUCTURE
HT\u TREE\u STRUCTURE\u ONE
HT\u TREE\u STRUCTURE\u TWO
,每个表都有一个字段
ID


有什么解决方法吗?

因为
树结构
不是
@实体
只使用
@MappedSuperclass

@MappedSuperclass
public abstract class TreeStructure<T extends TreeStructure> {
@MappedSuperclass
公共抽象类树结构{
而不是父类的
@实体
@继承


您可以在中找到
@MappedSuperclass

您是否尝试将
@MappedSuperclass
用于类
树状结构
,而不是
@Entity
@heritation
@MappedSuperclass
没有自己的表,也不是实体。不,我不知道!它通过了所有测试我做的正是我想要的。非常感谢!太好了!我只是把它作为一个答案