versioning fails for onetomany collection holder

Posted by Alexander Vasiljev on Stack Overflow See other posts from Stack Overflow or by Alexander Vasiljev
Published on 2010-04-27T13:34:06Z Indexed on 2010/04/28 7:13 UTC
Read the original article Hit count: 359

given parent entity

@Entity
public class Expenditure implements Serializable {
...
    @OneToMany(mappedBy = "expenditure", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy()
    private List<ExpenditurePeriod> periods = new ArrayList<ExpenditurePeriod>();

    @Version
    private Integer version = 0;
...
}

and child one

@Entity
public class ExpenditurePeriod implements Serializable {
...
    @ManyToOne
    @JoinColumn(name="expenditure_id", nullable = false)
    private Expenditure expenditure;
...
}

While updating both parent and child in one transaction, org.hibernate.StaleObjectStateException is thrown: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):

Indeed, hibernate issues two sql updates: one changing parent properties and another changing child properties. Do you know a way to get rid of parent update changing child? The update results both in inefficiency and false positive for optimistic lock. Note, that both child and parent save their state in DB correctly.

Hibernate version is 3.5.1-Final

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about versioning