覆盖休眠验证器注释?

发布于 2024-08-21 23:00:14 字数 1135 浏览 7 评论 0原文

我想覆盖休眠验证器中的约束。这是我的基类:

@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 但它似乎只在列覆盖的情况下运行,而不是这种属性。

我该怎么办?

谢谢

I would like to override a constraint in hibernate validator. Here is my base class:

@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;
    }
}

I've got a child class where I want to specify that the equationType for equation field is EquationType.ANOTHER_EQUATION, something like this:

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

}

this code throw an exception saying that I've got a org.hibernate.MappingException: Repeated column in mapping for entity (normal, due to the duplicate getter)

There is an @AttributeOverride in Hibernate/jpa but it seems to run only with column override and not this kind of attribute.

How can I do it ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-08-28 23:00:14

嗯...我找到了一个实现它的技巧...这不是很干净...但它运行:

这是我的基类:

@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,因此重写的 getEquation 将用于方程字段映射)。

这是一个子类示例:

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

}

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

Well... I've found a tip to achieve it... That's not very clean... but it runs:

here is my base class:

@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;
    }
}

You need to specify the getter as @Transient because if not, Hibernate will throw a duplicate definition for the equation field. My equation field is now protected and not private.

And now, on each of your subclass, you override the getEquation by specifying the constraint (and as @Transient is not inherited by subclasses, the overriden getEquation will be use for the equation field mapping).

Here is a subclass example:

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

}

I haven't found better...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文