English 中文(简体)
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?

问题回答

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() );
        }
    

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.





相关问题
query must begin with SELECT or FROM: delete [delete

I am using JPA in addition to spring(3.0.0.M4). While deleting multiple records using query.executeUpdate() i am getting the following exception. org.springframework.web.util.NestedServletException: ...

Last update timestamp with JPA

I m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that s supposed to reflect whenever that entity was last updated. What are the strategies for making ...

@IdClass with non primative @Id

I m trying to add a composite primary key to a class and having a bit of trouble. Here are the classes. class User { private long id; ... } class Token { private User user; private ...

Database not dropped in between unit test

Hello good people i came accross a weird behaviour in my test.I m using JPA hibernate annotation with spring. let say i have an Class MyObject and it s property email is marqued @Column(name="EMAIL", ...

Toplink trying to persist null object

I have an object "Instance" with another object "Course" inside. When trying to persist a new Instance object, I get the following error if Course is null: java.lang.IllegalStateException: During ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签