Question

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

Was it helpful?

Solution

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top