gpt4 book ai didi

java - Hibernate映射子类忽略父类

转载 作者:行者123 更新时间:2023-12-01 14:42:41 25 4
gpt4 key购买 nike

我想将许多子类映射到一个公共(public)父类:B扩展A,C扩展A,...但数据库不关心A。并且B和C没有任何共同点(没有Id,没有引用...)。

public class A {
@Id
@Column(name="id")
private Long id;

@Column(name="reference", nullable=false)
private String reference;

}

是否可以在不添加@Entity的情况下做到这一点?

解决方案

@MappedSuperclass
public class A {
@Id
@Column(name="id")
private Long id;

@Column(name="reference", nullable=false)
private String reference;

}

@Entity
@Table(name="B")
public class B extends A {
}

最佳答案

使用@MappedSuperclassA类上:

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

关于java - Hibernate映射子类忽略父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com