I get the following error when attempting to persist an object:
java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST
Is there any simple way to tell which relationship has the problem object?
It is possible that the persisted object has many relationships and a trial and error or find by elimination works, but I would like to know if there is a simpler quicker way to identify the problem relationship object.
Update: I encounter this problem periodically, and I have always been able to fin开发者_StackOverflow社区d the source of the problem, or have been able to reorder the operations in order to solve the problem, but my issue is the amount of time that it takes to find the offending object.
My solutions have always been found by trial and error. I sometimes find the solution in minutes, but sometimes it takes hours. My question is: is there an easier way to find which of possibly many relationships is causing the problem. The exception only claims that a "new object" was found through a "relationship", this does not help me to find which object or which relationship. Is there a log or a way to tell the system to provide a more specific error?
The next is a listener for all the request of your APP, to activate it :
In your
persistence.xml
add:<property name="eclipselink.session.customizer" value="dz.bilelovitch.QueryListener"/>
Create the
QueryListener
:import org.eclipse.persistence.config.SessionCustomizer; import org.eclipse.persistence.sessions.Session; public class QueryListener implements SessionCustomizer { @Override public void customize(Session aSession) throws Exception { System.out.println("log " + aSession.getLog() ); }
The answer is simple: debugging and testing. JUnit/TestNG and debugging mode should be your friends. There's nothing special with this error.
Moreover, Cascade.PERSIST may be missing, therefore you get that exception.
More info: http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Cascading
<property name="eclipselink.logging.level" value="FINEST"/>
will cause EclipseLink to show more detailed logs which can help you to detect the reason of error more closely.
精彩评论