I've spent too much time today debugging a problem in a test application which uses the javax.persistence API.
The problem is that I added about 15 new persistent classes, and got the following exception:
java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST
How useful is that? Why couldn't the exception contain the object which wasn't marked PERSIST? In which of the all the classes I added did I make an error?
Much, much later, I found the root cause. I had forgotten to add cascade=CascadeType.ALL on one of the
@OneToMany annotations!
This is how it looked:
@OneToMany(mappedBy="foo") private Set<Foo> foo = new HashSet<Foo>();
And by adding cascade=CascadeType.ALL it works again:
@OneToMany(mappedBy="foo", cascade=CascadeType.ALL) private Set<Foo> foo = new HashSet<Foo>();
Grrrrrr!
[Technorati Tags: Java ]





