MappedSuperclass将所有子实体映射到一个子实体

时间:2019-02-28 09:00:24

标签: spring jpa generics mappedsuperclass

我有 TranslationBase 基本抽象类,还有两个子类[entities]:CountryTranslation和RegionTranslation,如下所示:

TranslationBase:

@MappedSuperclass
@IdClass(TranslationBasePK.class)
public abstract class TranslationBase<T> implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "id")
    private T ownerEntity;

    @Id
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Column(name = "language_id")
    private Long languageId;

    private String name;
}

国家翻译

@Entity
@Table(name = "country_translation")
public class CountryTranslation extends TranslationBase<Country> {
}

RegionTranslation

@Entity
@Table(name = "region_translation")
public class RegionTranslation extends TranslationBase<Region> {
}

和TranslationBasePK:

public class TranslationBasePK implements Serializable {

    private Long ownerEntity;

    private Long languageId;
}

在数据库上,我在CountryTranslation和RegionTranslation表中找到了对Country表ID的引用的ID列!

我希望CountryTranslation引用Country的ID,而RegionTranslation引用Region类的ID。

注意:国家和地区的@Id地名命名为id;

0 个答案:

没有答案