Finding a needle in a haystack

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: ]

Comments:

Hmm looks like we haven't learned from the past. How many times haven't we battled with bad SQLException messages. Like the famous Oracle: table or view does not exist. Maybe the JPA Spec should concentrate on ease of debugging next time ;). All the great web frameworks compete on "line precise error reporting". Maybe the JPA imps. should do that too.

Posted by guest on February 12, 2007 at 02:31 AM PST #

Post a Comment:
Comments are closed for this entry.
About

martin

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today