|Re: Cascade.Persist problems [message #508030 is a reply to message #507809]
||Fri, 15 January 2010 15:44
| Chris Delahunt
Registered: July 2009
The problem is that you are using detached entities. Calilng persist on a detached entity is not allowed and results in the exception. When it the cascade option is missing and pair references an existing Foo, EclipseLink will use the primary key to populate the foreign key in Pair, but that is all - no changes in Foo will get picked up. When Foo is new, you get an exception because it tries to set the foreign key to a value that doesn't exist in Foo.
Setting the cascade option has the opposite effect because (I believe anyway) persist is specific to inserts and it is an app error to try to insert an existing object.
What you can use instead is Merge, which will operate as you expected persist to. Merge will determine at each level if the object is existing or new, so it will work the same for Pair. If you have cascade merge set, it will then cascade to Foo and do the same - when it encounters an existing but detached foo though, it will merge it and any changes into the existing copy of the Foo object instead of throwing the exception. The major application difference between merge and persist that you will have to account for though is that Merge returns the managed object, where as persist causes the object passed into become managed. This means that any changes made afterward will need to be made on the object tree returned from Merge - changes made to the object passed into it will not get picked up like they would if you continue to use the object passsed into persist.
Hope this helps.
Powered by FUDForum
. Page generated in 0.14568 seconds