hibernate 无法通过Map超类的属性进行Map

mv1qrgav  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(198)

实体:

@Entity
@Table(name = "ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item extends Base {
    @OneToOne(mappedBy = "item")
    protected Doc doc;
}

@MappedSuperclass
public abstract class Doc extends BaseDoc {
@OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "itemId")
    private Item item;
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseDoc extends Base {}

表格:

BASEDOC
- itemId int8
(other attributes)
ITEM
(other attributes)
BASE
(other attributes)

在运行时,它会失败,并显示:
原因:

org.hibernate.AnnotationException: Unknown mappedBy in: com.ghiton.updater.entity.Item.doc, referenced property unknown: com.ghiton.updater.entity.Doc.item"}}

我认为原因是MappedSuperclass,因为"item"存储在Base表中。有解决这类情况的实践吗?
我发现"Map的超类不能成为实体关系的目标",在这种情况下,我如何才能实现将该文档持久化到BaseDoc表中呢?
在数据库级别,它具有所需的所有列,因此不需要单独的DOC表。

92vpleto

92vpleto1#

不能将Map超类注解类与实体类联接。Map超类不是实体
Link
我想你可以这样修改你的代码。

@Entity
@Table(name = "ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item extends Base {
    @OneToOne(mappedBy = "item")
    protected Doc doc;
}

@Entity
@Table(name = "DOC")
public abstract class Doc extends BaseDoc {
    @OneToOne(mappedBy = "doc")
    private Item item;
}

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseDoc extends Base {}

相关问题