首页 > 解决方案 > EmbeddableId 应该映射为 insert="false" update="false"

问题描述

我有以下实体:Match带有 embeddableIdMatchKey和多态实体OrganisationMatch

休眠状态正在爆炸Repeated column in mapping for entity: net.satago.web.entities.OrganisationMatch column: referenceKind (should be mapped with insert="false" update="false")

不知道怎么回事,能不能不@DiscriminatorColumn在s部分使用注解@EmbeddableId,使它们不可插入也不可更新?

如果要区分的列不是实体的一部分@EmbeddableId而只是Match实体上的常规列,则它可以正常工作。

@Embeddable
@ParametersAreNonnullByDefault
public class MatchKey implements Serializable
{
    private static final long serialVersionUID = 7619427612022530146L;

    @Column(insertable = false, updatable = false)
    @Enumerated(STRING)
    private MatchableEntityKind referenceKind;

    private Long referenceId;

    public MatchKey()
    {
        // For JPA
    }

    public MatchKey(OrganisationId organisationId)
    {
        this.referenceKind = ORGANISATION;
        this.referenceId = organisationId.getId();
    }

    public MatchableEntityKind getReferenceKind()
    {
        return referenceKind;
    }

    public void setReferenceKind(MatchableEntityKind referenceKind)
    {
        this.referenceKind = referenceKind;
    }

    public Long getReferenceId()
    {
        return referenceId;
    }

    public void setReferenceId(Long referenceId)
    {
        this.referenceId = referenceId;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof MatchKey)
        {
            MatchKey that = (MatchKey) obj;

            return this.referenceKind == that.referenceKind &&
                    Objects.equals(this.referenceId, that.referenceId);
        }

        return false;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(referenceKind, referenceId);
    }
}

@Entity
@Table(name = TABLE_NAME)
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "reference_kind", discriminatorType = DiscriminatorType.STRING)
@ParametersAreNonnullByDefault
public class Match implements EntityModel<MatchKey>
{
    static final String TABLE_NAME = "matches";

    @EmbeddedId
    private MatchKey id;

    @Version
    private Long version;

    ... generic match columns
}

@Entity
@DiscriminatorValue(OrganisationMatch.REFERENCE_KIND)
@ParametersAreNonnullByDefault
public class OrganisationMatch extends Match
{
    static final String REFERENCE_KIND = "ORGANISATION";

    @JoinColumn(name = "reference_id")
    @OneToOne(fetch = LAZY, optional = false)
    private Organisation organisation;

    public OrganisationMatch()
    {
        setReferenceKind(MatchableEntityKind.valueOf(REFERENCE_KIND));
    }

    public OrganisationMatch(OrganisationId organisationId)
    {
        super(new MatchKey(organisationId));
        setReferenceKind(MatchableEntityKind.valueOf(REFERENCE_KIND));
    }

    public Organisation getOrganisation()
    {
        return organisation;
    }
}

标签: javahibernatejpapolymorphism

解决方案


推荐阅读