【发布时间】:2018-03-01 17:12:00
【问题描述】:
我在我的项目中使用Spring Data Jpa 和Hibernate。
我有三张桌子:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
class Parent {
String id;
String name;
}
@Entity
class FirstChild extends Parent {
...
}
@Entity
class SecondChild extends Parent {
...
}
在我的逻辑的第一步,我应该在没有子类型的情况下保存 Parent object。
第二步,我知道它应该属于哪个Child 表。
例如:
Parent parent = parentRepository.findById("id");
FirstChild firstChild = new FirstChild();
firstChild.setId(parent.getId());
firstChild.setName(parent.getName());
parentRepository.save(firstChild);
但是当我执行 Hibernate save 时,它会抛出异常:
o.h.e.i.DefaultLoadEventListener Load request found matching entity in context, but the matched entity was of an inconsistent return type; returning null
据我了解,它不知道如何将 entity 从父类型升级到子类型,并且由于冲突而引发异常 - 具有相同 id 的实体已经存在。
这个问题有解决办法吗?
【问题讨论】:
标签: java spring hibernate jpa spring-data-jpa