You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Jerry Carter <je...@jerrycarter.org> on 2010/03/11 18:21:13 UTC

Refreshing detached entities

Possible gap in my understanding, but I would expect an exception in this case based on section 3.2.5 of the JPA 2.0 specification: "If [entity] X is new, detached, or removed entity, the IllegalArgumentException is thrown".

try {
	TestEntity refreshDetached = new TestEntity("refresh detached");
	em.persist(refreshDetached);
	em.flush();
	em.detach(refreshDetached);
	em.refresh(refreshDetached);
	logger.error("ERROR: No exception from em.refresh(detached)");
} catch (IllegalArgumentException e) {
	logger.info("SUCCESS: Refresh detached");
} catch (Exception e) {
	logger.error("ERROR: Unexpected exception from em.refresh(detached): " + e.toString());
}

ERROR: No exception from em.refresh(detached)



Re: Refreshing detached entities

Posted by Dianne Richards <di...@gmail.com>.
Jerry - I'll look into this.

Dianne

On Thu, Mar 11, 2010 at 11:21 AM, Jerry Carter <je...@jerrycarter.org>wrote:

>
> Possible gap in my understanding, but I would expect an exception in this
> case based on section 3.2.5 of the JPA 2.0 specification: "If [entity] X is
> new, detached, or removed entity, the IllegalArgumentException is thrown".
>
> try {
>        TestEntity refreshDetached = new TestEntity("refresh detached");
>        em.persist(refreshDetached);
>        em.flush();
>        em.detach(refreshDetached);
>        em.refresh(refreshDetached);
>        logger.error("ERROR: No exception from em.refresh(detached)");
> } catch (IllegalArgumentException e) {
>        logger.info("SUCCESS: Refresh detached");
> } catch (Exception e) {
>        logger.error("ERROR: Unexpected exception from em.refresh(detached):
> " + e.toString());
> }
>
> ERROR: No exception from em.refresh(detached)
>
>
>


-- 
Thanks - Dianne

Re: Refreshing detached entities

Posted by No1UNo <je...@jerrycarter.org>.
Thanks, Donald and Fay.  Your guess was exactly right.  There was a bug in Spring 3.0.1 that caused 'PersistenceVersion' to always be set to 1.0 <http://jira.springframework.org/browse/SPR-6975>.  With a fix in place, I am now seeing the expected behavior in OpenJPA for every case save one:

	try {
		TestEntity refreshRemoved = new TestEntity("refresh removed");
		em.persist(refreshRemoved);
		em.flush();
		em.remove(refreshRemoved);
		em.flush();
		em.refresh(refreshRemoved);
		logger.error("ERROR: No exception from em.refresh(removed)");
	} catch (IllegalArgumentException e) {
		logger.info("SUCCESS: Refresh removed");
	} catch (Exception e) {
		logger.error("ERROR: Unexpected exception from em.refresh(removed): " + e.toString());
	}

I've updated OPENJPA-1562 accordingly.


On Mar 11, 2010, at 3:46 PM, DWoods [via OpenJPA] wrote:
> Is Spring 3.0 supplying a JPA 1.0 implementation and/or API on the 
> classpath? 
> 
> I'm not a Spring user, so no real help here... 
> 
> -Donald 


-- 
View this message in context: http://n2.nabble.com/Refreshing-detached-entities-tp4717312p4720364.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Refreshing detached entities

Posted by Donald Woods <dw...@apache.org>.
Is Spring 3.0 supplying a JPA 1.0 implementation and/or API on the
classpath?

I'm not a Spring user, so no real help here...


-Donald


On 3/11/10 3:10 PM, No1UNo wrote:
> 
> Very interesting data point.  I just added a simple test to OPENJPA-1562, but
> that test contains Spring 3.0 and uses a RESOURCE_LOCAL <persistence-unit>. 
> I'll try eliminating Spring 3.0.

Re: Refreshing detached entities

Posted by No1UNo <je...@jerrycarter.org>.
Very interesting data point.  I just added a simple test to OPENJPA-1562, but
that test contains Spring 3.0 and uses a RESOURCE_LOCAL <persistence-unit>. 
I'll try eliminating Spring 3.0.
-- 
View this message in context: http://n2.nabble.com/Refreshing-detached-entities-tp4717312p4718371.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Refreshing detached entities

Posted by Fay Wang <fy...@yahoo.com>.
hi Jerry,
       I could not reproduce this problem and I am using the trunk level code.

11256  callback  TRACE  [main] openjpa.jdbc.SQL - <t 16785420, conn 20448186> executing prepstmnt 2721032 INSERT INTO EntityA (id) VALUES (?) [params=(int) 1]
11276  callback  TRACE  [main] openjpa.jdbc.SQL - <t 16785420, conn 20448186> [20 ms] spent
java.lang.IllegalArgumentException: refresh can not be invoked on "callback.EntityA-1". This entity is either detached or not persistent or null.
    at org.apache.openjpa.persistence.EntityManagerImpl.assertValidAttchedEntity(EntityManagerImpl.java:1344)
    at org.apache.openjpa.persistence.EntityManagerImpl.refresh(EntityManagerImpl.java:744)
    at org.apache.openjpa.persistence.EntityManagerImpl.refresh(EntityManagerImpl.java:731)
    at callback.TestCallback.testRefreshOnDetachedEntity(TestCallback.java:68)
    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 junit.framework.TestCase.runTest(Unknown Source)
    at junit.framework.TestCase.runBare(Unknown Source)
    at junit.framework.TestResult$1.protect(Unknown Source)
    at junit.framework.TestResult.runProtected(Unknown Source)SUCCESS: Refresh detached

Here is my test code snippet:
    public void testRefreshOnDetachedEntity() { 
        try {
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            EntityA a = new EntityA();
            a.setId(2);
            em.persist(a);
            em.flush();
            em.detach(a);
            em.refresh(a);
            System.err.println("ERROR: No exception from em.refresh(detached)");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            System.out.println("SUCCESS: Refresh detached");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("ERROR: Unexpected exception from em.refresh(detached): " + e.toString());
        }            
    }





----- Original Message ----
From: Jerry Carter <je...@jerrycarter.org>
To: users@openjpa.apache.org
Sent: Thu, March 11, 2010 9:21:13 AM
Subject: Refreshing detached entities


Possible gap in my understanding, but I would expect an exception in this case based on section 3.2.5 of the JPA 2.0 specification: "If [entity] X is new, detached, or removed entity, the IllegalArgumentException is thrown".

try {
    TestEntity refreshDetached = new TestEntity("refresh detached");
    em.persist(refreshDetached);
    em.flush();
    em.detach(refreshDetached);
    em.refresh(refreshDetached);
    logger.error("ERROR: No exception from em.refresh(detached)");
} catch (IllegalArgumentException e) {
    logger.info("SUCCESS: Refresh detached");
} catch (Exception e) {
    logger.error("ERROR: Unexpected exception from em.refresh(detached): " + e.toString());
}

ERROR: No exception from em.refresh(detached)