继承映射注释

时间:2012-05-16 09:12:30

标签: java hibernate inheritance jpa

我正在开发一个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中定义的映射继承到每个子类中。 可能吗?我必须在我的超级课程中做些什么改变,以及我需要在我的子实体中添加什么?

注意所有子类都将映射到同一个表中。

3 个答案:

答案 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的值