Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Eclipselink: How to find which relationship is causing error

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 find 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?

like image 388
rayd09 Avatar asked Nov 26 '22 12:11

rayd09


1 Answers

The next is a listener for all the request of your APP, to activate it :

  1. In your persistence.xml add:

    <property name="eclipselink.session.customizer" value="dz.bilelovitch.QueryListener"/>
    
  2. 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() );
        }
    
like image 137
bilelovitch Avatar answered Jan 06 '23 08:01

bilelovitch