persistence@glassfish.java.net

Re: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST.

From: Jon Miller <jemiller_at_uchicago.edu>
Date: Wed, 10 Jan 2007 20:22:31 -0600

Thanks Mitesh. I think you are correct (I am only updating one side of a
bidirectional relationship). What I've been trying to accomplish is to
somehow emulate Hibernate's delete-orphan/TopLink's private owned
relationship, but, do it in a provider independent manner. I'm currently
using the following code which seems to work fine in some cases but not
others. e.g. it works fine in some unit tests that I wrote, but, when I try
to use it in a larger app, it fails. If I uncomment em.clear() it works.
I'll have to think about it some more tomorrow. My brain is fried and I'm
thinking this approach isn't the way to go.

    public Schedule saveSchedule(Schedule schedule) {
        ...
        EntityManager em = EntityManagerHelper.getEntityManager();
// em.clear();
        EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            schedule = em.merge(schedule);
            List<Shift> l = new ArrayList<Shift>(schedule.getShifts());
            et.commit();
            et.begin();
            em.refresh(schedule);
            List<Shift> l2 = new ArrayList<Shift>(schedule.getShifts());
            for(Shift s : l2) {
                if(!l.contains(s)) {
                    schedule.getShifts().remove(s);
                    em.remove(s);
                }
            }
            et.commit();
        }
        catch(RuntimeException e) {
            if(et.isActive()) {
                et.rollback();
            }
            throw e;
        }
    }

Jon

----- Original Message -----
From: "Mitesh Meswani" <Mitesh.Meswani_at_Sun.COM>
To: <persistence_at_glassfish.dev.java.net>
Sent: Wednesday, January 10, 2007 8:02 PM
Subject: Re: java.lang.IllegalStateException: During synchronization a new
object was found through a relationship that was not marked cascade PERSIST.


> Hi Jon,
>
> A fix has been checked in
> <https://glassfish.dev.java.net/source/browse/glassfish/entity-persistence/src/java/oracle/toplink/essentials/internal/localization/i18n/ExceptionLocalizationResource.java?r1=1.14&r2=1.15>.
> The error message now includes the offending object. Today's nightly
> should contain the fix. Typically you get into this situation if you are
> not maintaining both sides of your relationships. For example if you have
> a domain model with Department and Employee with a bidirectional
> relationship, if you do emp.setDepartment(newDepartment), you will need to
> make sure to remove the employee from its old department's employees
> collection and add it to newDeapartment's employee collection.
>
> -Mitesh
>
>
> Jon Miller wrote:
>> Hi all,
>>
>> I'm running into the following exception. I'm wondering if anyone has
>> suggestions on how to find the offending object? I'm thinking it might be
>> nice if the error message did something like print the toString()
>> representation of the offending object.
>>
>> Caused by: javax.faces.el.EvaluationException:
>> javax.persistence.RollbackException: java.lang.IllegalStateException:
>> During synchronization a new object was found through a relationship that
>> was not marked cascade PERSIST.
>>
>> Jon
>