Configuration: Spring 2.5.6, Hibernate 3.6.0
Supposed I have 2 objects:
where Cage owns a set of Lion.
Here's the annotation on Cage:
Code:
@OneToMany(fetch=FetchType.EAGER) @JoinColumn(name="owner_id")
@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.DELETE_ORPHAN, org.hibernate.annotations.CascadeType.ALL})
private Set<Lion> getLions()
Here's what I'm trying to do / how I'm reproducing the bug:
1. Create a transient Cage that has a single object in its set, Lion
2. Attempt to persist Cage - a validation exception is thrown from within the transaction, causing the transaction to rollback
3. Fix the validation error
4. Attempt to persist Cage again - This time the transaction completes successfully, but the owner_id entry in Lion is missing. Orphaned Lion without a cage is not good!
From some searching, it sounds like hibernate and rollback do not work well together. I'm trying to understand why, and if there's a work-around (other than performing a deep-copy beforehand).
Why would this be happening? Each of the 2 persist attempts is a separate context (I'm seeing a separate log message "org.hibernate.impl.SessionImpl - opened session at timestamp XXXXX" at the beginning of each JPA transaction), and the Java object doesn't appear to be changed after the first/failed commit.
This means that there must be state about the collection being stored elsewhere? Any tips on where to look?
I also observed:
When the relationship is a SET, the owner_id is missing.
When the relationship is a LIST, the row in the join table is missing.
Thanks!
Mike
Related topics / articles:
http://weblogs.java.net/blog/2007/04/27/jpa-and-rollbacks-not-prettyhttps://forum.hibernate.org/viewtopic.php?t=943975