我正在开发一个JPA项目,其中我有一些不同的实体扩展了一个注释为Entity
的超类:
@Entity
@Table(name = "export_profiles")
@NamedQueries({
@NamedQuery(name = "ExportProfile.getAll", query = "select ep from PersistentExportProfile ep"),
@NamedQuery(name = "ExportProfile.getByName", query = "select ep from PersistentExportProfile ep where ep.profileName = :name") })
public abstract class PersistentExportProfile extends AbstractExportProfile {
// other mappings...
}
我想将我PersistentExportProfile
中定义的映射继承到每个子类中。
可能吗?我必须在我的超级课程中做些什么改变,以及我需要在我的子实体中添加什么?
注意所有子类都将映射到同一个表中。
答案 0 :(得分:0)
如果超类的唯一目的是为子类定义公共映射,但本身不是持久性的,那么最好使用@MappedSuperclass
注释或<mapped-superclass>
进行xml映射。有一个例子here。
答案 1 :(得分:0)
这种情况在Postgres可能是一个好的开始。
以示例
CREATE TABLE "public"."abstract_export_profile" (
"label" TEXT
) WITHOUT OIDS;
CREATE TABLE "public"."persistent_export_profile" (
"id" BIGSERIAL,
"value" TEXT,
CONSTRAINT "mandant_pkey" PRIMARY KEY("id")
) INHERITS ("public"."abstract_export_profile")
WITHOUT OIDS;
名称等级:
@MappedSuperclass
public class AbstractExportProfile { ... }
和
@Entity
@Table(name= "mandant")
public class PersistentExportProfile extends AbstractExportProfile { ... }
答案 2 :(得分:0)
对我来说最好的解决方案是将@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
和@DiscriminatorColumn(name="export_type", discriminatorType=DiscriminatorType.STRING)
添加到我的抽象类中,然后在我的具体类中添加@DiscriminatorValue
来定义DiscriminatorColumn
的值