Hi Alex,

To delete an instance you need to be in a transaction. If you are outside of a transaction, you get detached instances for everything.

If you begin a transaction, either directly or via a framework annotation, you should not use merge, but directly remove the instance you found.

@Transactional // made this up
    public boolean delete(final int id) {
final JpaTemplate t = getJpaTemplate(); // or ask for a transactional instance here
        T obj = t.find(getObjectClass(), id);
        if (obj != null) {
            t.remove(obj);
return true;
        }
return false;
    }

Craig

On Oct 22, 2007, at 9:03 AM, AlexD. wrote:


Hallo all,

I have the problem that I cannot delete a detached object.

My source codes are (I'm using the spring framework):

    public void delete(final int id) {
        final JpaTemplate t = getJpaTemplate();
        T obj = t.find(getObjectClass(), id);
        if (obj != null) {
            obj = t.merge(obj);
            t.remove(obj);
        }
    }


Looks strange, but obj what I got as result is also detached object... What
I'm doing wrong?

stack trace:

 org.apache.openjpa.persistence.ArgumentException: You cannot perform
operation delete on detached object
"org.apache.openjpa.enhance.com$btelligent$erp$domain$persistence [EMAIL PROTECTED]".
This operation only applies to managed objects.
FailedObject:
org.apache.openjpa.enhance.com$btelligent$erp$domain$persistence [EMAIL PROTECTED]
        at
org.apache.openjpa.kernel.BrokerImpl.newDetachedException (BrokerImpl.java:2521)
        at org.apache.openjpa.kernel.BrokerImpl.delete(BrokerImpl.java:2512)
        at org.apache.openjpa.kernel.BrokerImpl.delete(BrokerImpl.java:2474)
        at
org.apache.openjpa.kernel.DelegatingBroker.delete (DelegatingBroker.java:1030)
        at
org.apache.openjpa.persistence.EntityManagerImpl.remove (EntityManagerImpl.java:555)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.springframework.orm.jpa.ExtendedEntityManagerCreator $ExtendedEntityManagerInvocationHandler.invoke (ExtendedEntityManagerCreator.java:340)
        at $Proxy48.remove(Unknown Source)
at org.springframework.orm.jpa.JpaTemplate$7.doInJpa (JpaTemplate.java:280) at org.springframework.orm.jpa.JpaTemplate.execute (JpaTemplate.java:191) at org.springframework.orm.jpa.JpaTemplate.remove(JpaTemplate.java: 278) at com.btelligent.erp.dao.jpa.AbstarctJpaDao.delete (AbstarctJpaDao.java:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectio n(AopUtils.java:296)
        at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke (JdkDynamicAopProxy.java:198)
        at $Proxy20.delete(Unknown Source)


Best regards,

Alex
--
View this message in context: http://www.nabble.com/Cannot-perform- operation-delete-on-detached-object-tf4670739.html#a13342865
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:[EMAIL PROTECTED]
P.S. A good JDO? O, Gasp!

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to