I’m writing this to keep memory of an issue that took me quite some time to resolve.
I don’t claim this is the right way to solve this issue in each and every case… it just worked for me
Suppose you have the following multi-level schema in a Bus service application:
-
Line
** name
** waypoints
** shifts -
Waypoint
** address
** lat
** lng -
Shift
** name
** timeTable (list of Stops) -
Stop
** arrivalTime
** waypoint
All relationships have oneToMany card. and ASSOCIATION type.
Now you’re trying to build a browser-edit screen where you have all the informations about a Line and you can add new Shifts to the list.
You add a tabSheet and Tabs for Waypoints and Shifts
You create a dialog Edit for Shifts with a groupTable for the timeTable and of course dialog Edit screens for Waypoints and Stops.
After deploy, you create a new Line, some Waypoints and you try to create a new Shift.
The first dialog opens, you want to define the timeTable so you click on the Create button to open the Stop.edit screen, you fill-up the form click ok:
BAM!
IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST:
what is happening here?
As @knstvk explains here the shift is not saved in the same persistence context (transaction) as the stops in the timetable so when the last open Edit screen is committed it finds a Shift in a ‘new’ state which cannot be committed since there is no CASCADE relationship between Stop and Shift.
This particular case can be solved if we think that the timeTable is actually a COMPOSITION of Stops so if we annotate it with @Composition we get the cascade persistence and the error is gone!
The topic is open for corrections