Discriminator Column可以成为Doctrine2中主键的一部分吗?

时间:2010-09-26 20:51:19

标签: php oop orm single-table-inheritance doctrine-orm

我在Doctrine2中使用Single Table Inheritance来存储多个服务的OAuth凭据。我想使用服务的id作为主键;但是,这并非所有服务都是唯一的。

我已经设置数据库使用鉴别器列和服务的id作为主键,但我找不到让Doctrine使用鉴别器列作为键的方法(除了鉴别器列)。我正在使用docblock注释,如果我将discriminator列添加为@Id字段,我会收到错误:

Duplicate definition of column...in a field or discriminator column mapping.

如果我只将字段定义为鉴别器列,则任何重叠的服务ID都会更新所有匹配的行。

无论如何要做到这一点,除了使用自动生成它的价值?

2 个答案:

答案 0 :(得分:2)

您不能,descriminator列不能用作主键的一部分。

为什么这个用例btw需要STI?你必须为你提供的每个open-id服务创建一个新类,听起来很烦人: - )

答案 1 :(得分:2)

对于那些使用Hibernate的人,你可以(至少从JPA 2.1开始)。以下代码perfeclty适用于我的环境(hibernate-entitymanager 4.3.6.Final):

@Entity
@Table(name = "CODIFICATIONS")
@IdClass(CodificationId.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = Codification.DISCRIMINATOR_COLUMN, discriminatorType = DiscriminatorType.INTEGER)
public abstract class Codification implements Serializable {

    public static final String DISCRIMINATOR_COLUMN = "TABLE_ID";

    private static final long serialVersionUID = 1L;

    @Column(name = "CODIFICATION_ID")
    protected Long codificationId;

    @Id
    @Column(name = DISCRIMINATOR_COLUMN, insertable = false, updatable = false)
    protected Long tableId;

    @Id
    @Column(name = "CODE_ID", insertable = false, updatable = false)
    protected Long codeId;

    @Column(name = "LONG_NAME")
    protected String longName;

    @Column(name = "SHORT_NAME")
    protected String shortName;

}

public class CodificationId implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long tableId;
    private Long codeId;

    public Long getTableId() {
        return tableId;
    }

    public void setTableId(Long tableId) {
        this.tableId = tableId;
    }

    public Long getCodeId() {
        return codeId;
    }

    public void setCodeId(Long codeId) {
        this.codeId = codeId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((tableId == null) ? 0 : tableId.hashCode());
        result = prime * result + ((codeId == null) ? 0 : codeId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CodificationId other = (CodificationId) obj;
        if (tableId == null) {
            if (other.tableId != null)
                return false;
        } else if (!tableId.equals(other.tableId))
            return false;
        if (codeId == null) {
            if (other.codeId != null)
                return false;
        } else if (!codeId.equals(other.codeId))
            return false;
        return true;
    }

}

@Entity
@DiscriminatorValue(Status.DISCRIMINATOR_VALUE)
public class Status extends Codification {

    public static final String DISCRIMINATOR_VALUE = "2";

    private static final long serialVersionUID = 1L;

}

然后我使用以下代码配置与Status的关联:

@ManyToOne
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = Codification.DISCRIMINATOR_COLUMN, value = Status.DISCRIMINATOR_VALUE)),
        @JoinColumnOrFormula(column = @JoinColumn(name = "STATUS", referencedColumnName = "CODE_ID")) })
private Status status;