Hibernate注释继承

时间:2016-02-18 07:30:00

标签: java spring hibernate hibernate-annotations

我正在尝试扩展Agent类以添加新字段。 Agent类有一个字段,在sql-server中定义为not-allow nulls,但在db中有一个默认的'45'。

问题是当我尝试保存并提交一些更改时,如果我不使用扩展,那么一切都很好,并且在db中定义的默认“45”真的有效,即使我没有对该字段赋值,其值为'45'。

当我使用扩展时,我真的好奇为什么,默认值'45'不起作用,我得到了数据完整性异常。

这是一个代码示例:

    Array
    (
        [0] => akcja
        [1] => sci-fi
    )

    Array
    (
        [0] => akcja   / Incorect value added from first array
        [1] => sci-fi  /  Incorect value added from first array
        [2] => melodramat    
        [3] => sci-fi
    )

我得到的例外是:

@Entity
@Table(name = "a_agent_main")
@AttributeOverride(name = "id", column = @Column(name = "agent_no"))
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorOptions(insert=false)
public class Agent extends AbstractRevisionedBusinessIdEntity implements ItemEntityValuesContainer {

    @Column(name = "payment_delay") // this is the column that in the db '45' it's his default value
    private Integer paymentDelay;

    public Integer getPaymentDelay() {
        return this.paymentDelay;
    }

    public void setPaymentDelay(Integer paymentDelay) {
        this.paymentDelay = paymentDelay;
    }
}


@Entity
@DiscriminatorValue("1")
public class AgentExt extends Agent{        
    @Column(name = "z_agent_group_code")
    private Integer agentGroupCode;

    public Integer getAgentGroupCode() {
        return agentGroupCode;
    }

    public void setAgentGroupCode(Integer agentGroupCode) {
        this.agentGroupCode = agentGroupCode;
    }

}

0 个答案:

没有答案