-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Control Set's Primary Key
PostPosted: Thu Jul 09, 2015 11:38 am 
Newbie

Joined: Fri Nov 07, 2014 1:17 pm
Posts: 3
I have an Entity (RiskModel) that contains a Set of Embeddables (DiscreteTerms). Everything loads fine, but when I try to remove elements from the Set, Hibernate issues individual DELETE statements which fail to match anything because DiscreteTerm contains a floating-point field (coefficient). The WHERE clause doesn't actually match, I think due to floating-point precision issues.

I can think of two ways to solve this:
  1. Have Hibernate delete the whole collection and re-create it upon every modification. (This would be OK because this operation is rare.)
  2. Have Hibernate not include the coefficient in the WHERE clause. (This would be OK because the Set should not contain elements that differ only by coefficient.)

But I'm not sure how to make Hibernate do either of the above. Can anybody provide guidance? Or come up with a different solution?

Here is the relevant code:

Code:
@Entity
public class RiskModel implements Comparable<RiskModel>
{
    private Set<DiscreteTerm> fDiscreteTerms = new HashSet<>();

    // ... snip ...
   
    /**
     * <p>The {@link DiscreteTerm}s in the model's sum. Mutable.</p>
     */
    @ElementCollection(fetch = FetchType.EAGER) // eager-load due to close association
    // Override strange defaults.
    @CollectionTable(
            name = "risk_model_discrete_term",
            joinColumns = @JoinColumn(name = "risk_model_id"))
    public Set<DiscreteTerm> getDiscreteTerms()
    {
        return fDiscreteTerms;
    }

    /**
     * For reflection-based construction only. Business code should modify the
     * Set returned by {@link #getDiscreteTerms()}.
     */
    void setDiscreteTerms(Set<DiscreteTerm> disceteTerms)
    {
        fDiscreteTerms = disceteTerms;
    }

   // .. snip ...


Code:
@Embeddable
public final class DiscreteTerm extends SingleVariableTerm
{
    private float fCoefficient = 0.0f;
    private DiscreteVariable fVariable = null;
    private int fOptionIndex = -1;
   
    @Basic
    public float getCoefficient()
    {
        return fCoefficient;
    }

    public void setCoefficient(float coefficient)
    {
        fCoefficient = coefficient;
    }

    @Override
    @ManyToOne(optional = false, targetEntity = AbstractVariable.class)
    public DiscreteVariable getVariable()
    {
        return fVariable;
    }

    /**
     * For reflection-based construction only. The associated Variable should
     * not be changed.
     */
    void setVariable(final DiscreteVariable variable)
    {
        fVariable = Objects.requireNonNull(variable);
    }

    @Basic
    public int getOptionIndex()
    {
        return fOptionIndex;
    }

    /**
     * For reflection-based construction only. The associated option should not
     * be changed.
     */
    void setOptionIndex(final int optionIndex)
    {
        fOptionIndex = optionIndex;
    }
   
    @Override
    public boolean equals(final Object o)
    {
        // [snip], compares coefficient, variable, and optionIndex
    }
   
    @Override
    public int hashCode()
    {
        return Objects.hash(getCoefficient(), getVariable(), getOptionIndex());
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.