EmbeddableId应该映射为insert =“ false” update =“ false”

时间:2019-05-02 16:46:29

标签: java hibernate jpa polymorphism

我有以下实体:Match,具有embeddableId MatchKey和多态实体OrganisationMatch

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

我不知道出什么问题了,我不能在@DiscriminatorColumn的各个部分上使用@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;
    }
}

0 个答案:

没有答案