覆盖一个hibernate验证器注释?

时间:2010-02-16 17:06:22

标签: hibernate annotations override hibernate-validator

我想在hibernate验证器中覆盖一个约束。这是我的基类:

@Entity
@Table(name = "Value")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "valueType", discriminatorType = DiscriminatorType.STRING)
public abstract class Value extends CommonTable
{
    private String              equation;

    @SimpleEquation(equationType = EquationType.EQUATION)
    public String getEquation()
    {
        return equation;
    }

    public void setEquation(String equation)
    {
        this.equation = equation;
    }
}

我有一个子类,我想指定方程字段的equationType是EquationType.ANOTHER_EQUATION,如下所示:

@Entity(name = "CharacteristicUpgradeValue")
@DiscriminatorValue("CharacteristicUpgradeValue")
public class CharacteristicUpgradeValue extends Value
{    
    @Override
    @SimpleEquation(equationType = EquationType.COST_VALUE)
    public String getEquation()
    {
        return super.getEquation();
    }

}

此代码抛出一个异常,说我有一个org.hibernate.MappingException:实体映射中的重复列(正常,由于重复的getter)

Hibernate / jpa中有一个@AttributeOverride但它似乎只运行了列覆盖而不是这种属性。

我该怎么做?

感谢

1 个答案:

答案 0 :(得分:2)

嗯......我找到了一个实现它的小费......那不是很干净......但它运行起来了:

这是我的基类:

@Entity
@Table(name = "Value")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "valueType", discriminatorType = DiscriminatorType.STRING)
public abstract class Value extends CommonTable
{
    protected String                equation;

    @Transient
    public abstract String getEquation();

    public void setEquation(String equation)
    {
        this.equation = equation;
    }
}

你需要将getter指定为@Transient,因为如果没有,Hibernate会为方程字段抛出一个重复的定义。我的方程字段现在受到保护而不是私有。

现在,在每个子类上,通过指定约束来覆盖getEquation(并且由于@Transient不是由子类继承的,所以overriden getEquation将用于方程字段映射)。

这是一个子类示例:

@Entity(name = "CharacteristicUpgradeValue")
@DiscriminatorValue("CharacteristicUpgradeValue")
public class CharacteristicUpgradeValue extends Value
{    
    @Override
    @SimpleEquation(equationType = EquationType.COST_VALUE)
    public String getEquation()
    {
        return equation;
    }

}

我没有找到更好的......