You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "Henry Lai (JIRA)" <ji...@apache.org> on 2007/08/22 23:04:31 UTC

[jira] Created: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

EntityListener that modify property value of a entity, causes invalid state exception
-------------------------------------------------------------------------------------

                 Key: OPENJPA-327
                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
             Project: OpenJPA
          Issue Type: Bug
          Components: jpa
    Affects Versions: 1.0.0
         Environment: windows xp, jdk 5
code was build time enhanced
            Reporter: Henry Lai


entitylistener callback that modifies property value of the entity throws exception

The following test code produces the following exception

<1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)


	/**
	 * for entity with version field, and if the lifecycle listener such as
	 * pre-persist, post-persist handler modifies the entity
	 * then when flush is invoke, results in optimistic lock exception
	 * 
	 * this test failes in openjpa 0.9.6
	 * this test failes in openjpa 0.9.7
	 * this test failes in openjpa 1.0.0
	 * 
	 * This test case will past with either of following changes
	 * 1) comment out em.flush();
	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
	 *
	 */

	public void testMultipleInsertWithEntityListener(){
		
		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
		EntityManagerFactory factory = 
			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
						System.getProperties() );
		
        EntityManager em = factory.createEntityManager();
        em.getTransaction().begin();
        T1Entity e1 = new T1Entity();		
        T1Entity e2 = new T1Entity();		
        e1.setName("Mickey");
        e2.setName("Goofy");
        
        em.persist(e1);
        em.flush();			// works if this line is commented out
        
        em.persist(e2);
        em.getTransaction().commit();
        em.close();
	}





package ptp.test.issue1;

import java.sql.Timestamp;

public class T1EntityListener {

  static int count;
  int instNum;

  public T1EntityListener() {
	 instNum = count++;
    System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
  }

  public void preUpdate(Object entity) {
    audit( "preUpdate", entity);
  }

  public void postUpdate(Object entity) {
    audit( "postUpdate", entity);
  }

  public void prePersist(Object entity) {
	    audit( "prePersist", entity);
	  }

	  public void postPersist(Object entity) {
	    audit( "postPersist", entity);
	  }

  public void audit(String eventName, Object entity) {
    if (entity instanceof IAudit) {
      IAudit auditEntity = (IAudit) entity;
      System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());

      try {
        auditEntity.setModifyDateTime(createTimeStamp());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }
  
  private Timestamp createTimeStamp() {
	    return new Timestamp(System.currentTimeMillis());
	  }




-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


RE: [jira] Updated: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by Pinaki Poddar <pp...@bea.com>.
What happens if the entity is modified/audited in preUpdate? preUpdate
is more appropriate for the nature of operation because by the time
postUpdate callbck the data has been flushed.  

Pinaki Poddar
972.834.2865

-----Original Message-----
From: Henry Lai (JIRA) [mailto:jira@apache.org] 
Sent: Wednesday, August 22, 2007 4:09 PM
To: dev@openjpa.apache.org
Subject: [jira] Updated: (OPENJPA-327) EntityListener that modify
property value of a entity, causes invalid state exception


     [
https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jir
a.plugin.system.issuetabpanels:all-tabpanel ]

Henry Lai updated OPENJPA-327:
------------------------------

    Attachment: ptpissue1.zip

test case and mapping files to reproduce bug

> EntityListener that modify property value of a entity, causes invalid 
> state exception
> ----------------------------------------------------------------------
> ---------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5 code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity
throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error>
org.apache.openjpa.persistence.InvalidStateException: Attempt to set
column "T1ENTITY.VER_ID" to two different values: (class
java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when
you fail to set both sides of a two-sided relation between objects, or
when you map different fields to the same column, but you do not keep
the values of these fields in synch.
> 	at
org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at
org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener
such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock
exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the
orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new
PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test",
"ptp/test/issue1/persistence.xml",
> 						System.getProperties()
);
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is
commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" +
instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ",
event=" + eventName + ", entity=" + auditEntity + ", ver=" +
auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.

Re: [jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by Pinaki Poddar <pp...@apache.org>.
I just ran the test on trunk with Auto generation strategy against Derby.
The test ran -- i should not say pass -- because it does not assert anything
and I did not step through the test to see what is the tester's expectation
-- but it ran without any runtime exception.

All the callbacks was fired as per the System.out statements. 


Pinaki Poddar wrote:
> 
> Hi,
>   I ran it with Oracle because the test uses Database's native Sequence
> generation capability. 
>   Can you please describe what exactly you do on MySQL side to make the
> test run with MySQL which afaik do not have a native sequence generator
> such as in Oracle. 
>    I will run the test the with other generation strategy against MySQL --
> but my guess is this failure has got something to do with generation
> strategy being used and how a particular database handles it. And hence I
> did not modify that aspect of the test classses.
> 
> 
> 
> 
> JIRA jira@apache.org wrote:
>> 
>> 
>>     [
>> https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12675594#action_12675594
>> ] 
>> 
>> Adam Hardy commented on OPENJPA-327:
>> ------------------------------------
>> 
>> I see it with mySQL and Derby. 
>> 
>> 
>>> EntityListener that modify property value of a entity, causes invalid
>>> state exception
>>> -------------------------------------------------------------------------------------
>>>
>>>                 Key: OPENJPA-327
>>>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>>>             Project: OpenJPA
>>>          Issue Type: Bug
>>>          Components: jpa
>>>    Affects Versions: 1.0.0
>>>         Environment: windows xp, jdk 5
>>> code was build time enhanced
>>>            Reporter: Henry Lai
>>>         Attachments: ptpissue1.zip
>>>
>>>
>>> entitylistener callback that modifies property value of the entity
>>> throws exception
>>> The following test code produces the following exception
>>> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error>
>>> org.apache.openjpa.persistence.InvalidStateException: Attempt to set
>>> column "T1ENTITY.VER_ID" to two different values: (class
>>> java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when
>>> you fail to set both sides of a two-sided relation between objects, or
>>> when you map different fields to the same column, but you do not keep
>>> the values of these fields in synch.
>>> 	at
>>> org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
>>> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
>>> 	/**
>>> 	 * for entity with version field, and if the lifecycle listener such as
>>> 	 * pre-persist, post-persist handler modifies the entity
>>> 	 * then when flush is invoke, results in optimistic lock exception
>>> 	 * 
>>> 	 * this test failes in openjpa 0.9.6
>>> 	 * this test failes in openjpa 0.9.7
>>> 	 * this test failes in openjpa 1.0.0
>>> 	 * 
>>> 	 * This test case will past with either of following changes
>>> 	 * 1) comment out em.flush();
>>> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml
>>> file 
>>> 	 *
>>> 	 */
>>> 	public void testMultipleInsertWithEntityListener(){
>>> 		
>>> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
>>> 		EntityManagerFactory factory = 
>>> 			openJPA.createEntityManagerFactory("test",
>>> "ptp/test/issue1/persistence.xml",
>>> 						System.getProperties() );
>>> 		
>>>         EntityManager em = factory.createEntityManager();
>>>         em.getTransaction().begin();
>>>         T1Entity e1 = new T1Entity();		
>>>         T1Entity e2 = new T1Entity();		
>>>         e1.setName("Mickey");
>>>         e2.setName("Goofy");
>>>         
>>>         em.persist(e1);
>>>         em.flush();			// works if this line is commented out
>>>         
>>>         em.persist(e2);
>>>         em.getTransaction().commit();
>>>         em.close();
>>> 	}
>>> package ptp.test.issue1;
>>> import java.sql.Timestamp;
>>> public class T1EntityListener {
>>>   static int count;
>>>   int instNum;
>>>   public T1EntityListener() {
>>> 	 instNum = count++;
>>>     System.out.println("T1EntityListener=" + this + ", instance=" +
>>> instNum );
>>>   }
>>>   public void preUpdate(Object entity) {
>>>     audit( "preUpdate", entity);
>>>   }
>>>   public void postUpdate(Object entity) {
>>>     audit( "postUpdate", entity);
>>>   }
>>>   public void prePersist(Object entity) {
>>> 	    audit( "prePersist", entity);
>>> 	  }
>>> 	  public void postPersist(Object entity) {
>>> 	    audit( "postPersist", entity);
>>> 	  }
>>>   public void audit(String eventName, Object entity) {
>>>     if (entity instanceof IAudit) {
>>>       IAudit auditEntity = (IAudit) entity;
>>>       System.out.println("****T1EntityListener inst=" + instNum + ",
>>> event=" + eventName + ", entity=" + auditEntity + ", ver=" +
>>> auditEntity.getVerId());
>>>       try {
>>>         auditEntity.setModifyDateTime(createTimeStamp());
>>>       } catch (Exception e) {
>>>         throw new RuntimeException(e);
>>>       }
>>>     }
>>>   }
>>>   
>>>   private Timestamp createTimeStamp() {
>>> 	    return new Timestamp(System.currentTimeMillis());
>>> 	  }
>> 
>> -- 
>> This message is automatically generated by JIRA.
>> -
>> You can reply to this email to add a comment to the issue online.
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/-jira--Created%3A-%28OPENJPA-327%29-EntityListener-that-modify-property-value-of-a-entity%2C-causes-invalid-state-exception-tp215677p2366110.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: [jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  I ran it with Oracle because the test uses Database's native Sequence
generation capability. 
  Can you please describe what exactly you do on MySQL side to make the test
run with MySQL which afaik do not have a native sequence generator such as
in Oracle. 
   I will run the test the with other generation strategy against MySQL --
but my guess is this failure has got something to do with generation
strategy being used and how a particular database handles it. And hence I
did not modify that aspect of the test classses.




JIRA jira@apache.org wrote:
> 
> 
>     [
> https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12675594#action_12675594
> ] 
> 
> Adam Hardy commented on OPENJPA-327:
> ------------------------------------
> 
> I see it with mySQL and Derby. 
> 
> 
>> EntityListener that modify property value of a entity, causes invalid
>> state exception
>> -------------------------------------------------------------------------------------
>>
>>                 Key: OPENJPA-327
>>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>>             Project: OpenJPA
>>          Issue Type: Bug
>>          Components: jpa
>>    Affects Versions: 1.0.0
>>         Environment: windows xp, jdk 5
>> code was build time enhanced
>>            Reporter: Henry Lai
>>         Attachments: ptpissue1.zip
>>
>>
>> entitylistener callback that modifies property value of the entity throws
>> exception
>> The following test code produces the following exception
>> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error>
>> org.apache.openjpa.persistence.InvalidStateException: Attempt to set
>> column "T1ENTITY.VER_ID" to two different values: (class
>> java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when
>> you fail to set both sides of a two-sided relation between objects, or
>> when you map different fields to the same column, but you do not keep the
>> values of these fields in synch.
>> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
>> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
>> 	/**
>> 	 * for entity with version field, and if the lifecycle listener such as
>> 	 * pre-persist, post-persist handler modifies the entity
>> 	 * then when flush is invoke, results in optimistic lock exception
>> 	 * 
>> 	 * this test failes in openjpa 0.9.6
>> 	 * this test failes in openjpa 0.9.7
>> 	 * this test failes in openjpa 1.0.0
>> 	 * 
>> 	 * This test case will past with either of following changes
>> 	 * 1) comment out em.flush();
>> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml
>> file 
>> 	 *
>> 	 */
>> 	public void testMultipleInsertWithEntityListener(){
>> 		
>> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
>> 		EntityManagerFactory factory = 
>> 			openJPA.createEntityManagerFactory("test",
>> "ptp/test/issue1/persistence.xml",
>> 						System.getProperties() );
>> 		
>>         EntityManager em = factory.createEntityManager();
>>         em.getTransaction().begin();
>>         T1Entity e1 = new T1Entity();		
>>         T1Entity e2 = new T1Entity();		
>>         e1.setName("Mickey");
>>         e2.setName("Goofy");
>>         
>>         em.persist(e1);
>>         em.flush();			// works if this line is commented out
>>         
>>         em.persist(e2);
>>         em.getTransaction().commit();
>>         em.close();
>> 	}
>> package ptp.test.issue1;
>> import java.sql.Timestamp;
>> public class T1EntityListener {
>>   static int count;
>>   int instNum;
>>   public T1EntityListener() {
>> 	 instNum = count++;
>>     System.out.println("T1EntityListener=" + this + ", instance=" +
>> instNum );
>>   }
>>   public void preUpdate(Object entity) {
>>     audit( "preUpdate", entity);
>>   }
>>   public void postUpdate(Object entity) {
>>     audit( "postUpdate", entity);
>>   }
>>   public void prePersist(Object entity) {
>> 	    audit( "prePersist", entity);
>> 	  }
>> 	  public void postPersist(Object entity) {
>> 	    audit( "postPersist", entity);
>> 	  }
>>   public void audit(String eventName, Object entity) {
>>     if (entity instanceof IAudit) {
>>       IAudit auditEntity = (IAudit) entity;
>>       System.out.println("****T1EntityListener inst=" + instNum + ",
>> event=" + eventName + ", entity=" + auditEntity + ", ver=" +
>> auditEntity.getVerId());
>>       try {
>>         auditEntity.setModifyDateTime(createTimeStamp());
>>       } catch (Exception e) {
>>         throw new RuntimeException(e);
>>       }
>>     }
>>   }
>>   
>>   private Timestamp createTimeStamp() {
>> 	    return new Timestamp(System.currentTimeMillis());
>> 	  }
> 
> -- 
> This message is automatically generated by JIRA.
> -
> You can reply to this email to add a comment to the issue online.
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/-jira--Created%3A-%28OPENJPA-327%29-EntityListener-that-modify-property-value-of-a-entity%2C-causes-invalid-state-exception-tp215677p2366080.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


[jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Pinaki Poddar (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12672539#action_12672539 ] 

Pinaki Poddar commented on OPENJPA-327:
---------------------------------------

This test case passes with Oracle on trunk version of OpenJPA.


> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Michael Dick (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12729300#action_12729300 ] 

Michael Dick commented on OPENJPA-327:
--------------------------------------

Hi B.J. I'm going to reattach your patches to OPENJPA-732 and apply the fix under that JIRA. It's a shame that we didn't mark 732 as a duplicate of this one when we first fixed it but I don't want to have two separate issues which essentially fix the same problem (just different releases). It'll be a little easier to just track the single issue (at least for me). 

Thanks for the patches though!

> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>            Assignee: Michael Dick
>         Attachments: OPENJPA327-1.0.patch, OPENJPA327-1.2.patch, ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Assigned: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Michael Dick (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Michael Dick reassigned OPENJPA-327:
------------------------------------

    Assignee: Michael Dick

> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>            Assignee: Michael Dick
>         Attachments: OPENJPA327-1.0.patch, OPENJPA327-1.2.patch, ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Henry Lai (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henry Lai updated OPENJPA-327:
------------------------------

    Attachment: ptpissue1.zip

test case and mapping files to reproduce bug

> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Adam Hardy (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12675594#action_12675594 ] 

Adam Hardy commented on OPENJPA-327:
------------------------------------

I see it with mySQL and Derby. 


> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Adam Hardy (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12669593#action_12669593 ] 

Adam Hardy commented on OPENJPA-327:
------------------------------------

This bug completely disables auditing, or at least auditing based on a "last_modified" timestamp field with an entity listener. 

It prevents users from having pre-persist, pre-update, post-persist and post-update EntityListeners with versioning.

Very surprising that this issue is still outstanding in version 1.2.0 when to my untrained eye it seems as if it's not a difficult fix.

Here are a few of the threads discussing it, none of which give any hint of a fix in the pipeline:

http://n2.nabble.com/Does-anyone-meet-the-conflict-values-in-version-column--tt209013.html#none

http://n2.nabble.com/InvalidStateException%3A-Attempt-to-set-column-%22client.version%22-to-two-different-values-tt210609.html

http://n2.nabble.com/-jira--Created%3A-%28OPENJPA-327%29-EntityListener-that-modify-property-value-of-a-entity%2C-causes-invalid-state-exception-tt215677.html#a215679



> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "Michael Dick (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Michael Dick resolved OPENJPA-327.
----------------------------------

    Resolution: Fixed

Resolved under OPENJPA-732. 

> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>            Assignee: Michael Dick
>         Attachments: OPENJPA327-1.0.patch, OPENJPA327-1.2.patch, ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (OPENJPA-327) EntityListener that modify property value of a entity, causes invalid state exception

Posted by "B.J. Reed (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/OPENJPA-327?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

B.J. Reed updated OPENJPA-327:
------------------------------

    Attachment: OPENJPA327-1.0.patch
                OPENJPA327-1.2.patch

This test case is working in trunk.  Looks like the fix for https://issues.apache.org/jira/browse/OPENJPA-732 will work for this.  The above is the change to BrokerImpl and the test case....slight changes in what worked on trunk and the patch for 1.0.x and 1.2.x.

> EntityListener that modify property value of a entity, causes invalid state exception
> -------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-327
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-327
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 1.0.0
>         Environment: windows xp, jdk 5
> code was build time enhanced
>            Reporter: Henry Lai
>         Attachments: OPENJPA327-1.0.patch, OPENJPA327-1.2.patch, ptpissue1.zip
>
>
> entitylistener callback that modifies property value of the entity throws exception
> The following test code produces the following exception
> <1.0.0-SNAPSHOT-SNAPSHOT fatal user error> org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "T1ENTITY.VER_ID" to two different values: (class java.lang.Integer)"2", (class java.lang.Integer)"3" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(PrimaryRow.java:338)
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setObject(RowImpl.java:505)
> 	/**
> 	 * for entity with version field, and if the lifecycle listener such as
> 	 * pre-persist, post-persist handler modifies the entity
> 	 * then when flush is invoke, results in optimistic lock exception
> 	 * 
> 	 * this test failes in openjpa 0.9.6
> 	 * this test failes in openjpa 0.9.7
> 	 * this test failes in openjpa 1.0.0
> 	 * 
> 	 * This test case will past with either of following changes
> 	 * 1) comment out em.flush();
> 	 * 2) uncomment <post-update method-name="postUpdate"/> in the orm.xml file 
> 	 *
> 	 */
> 	public void testMultipleInsertWithEntityListener(){
> 		
> 		PersistenceProviderImpl openJPA = new PersistenceProviderImpl();
> 		EntityManagerFactory factory = 
> 			openJPA.createEntityManagerFactory("test", "ptp/test/issue1/persistence.xml",
> 						System.getProperties() );
> 		
>         EntityManager em = factory.createEntityManager();
>         em.getTransaction().begin();
>         T1Entity e1 = new T1Entity();		
>         T1Entity e2 = new T1Entity();		
>         e1.setName("Mickey");
>         e2.setName("Goofy");
>         
>         em.persist(e1);
>         em.flush();			// works if this line is commented out
>         
>         em.persist(e2);
>         em.getTransaction().commit();
>         em.close();
> 	}
> package ptp.test.issue1;
> import java.sql.Timestamp;
> public class T1EntityListener {
>   static int count;
>   int instNum;
>   public T1EntityListener() {
> 	 instNum = count++;
>     System.out.println("T1EntityListener=" + this + ", instance=" + instNum );
>   }
>   public void preUpdate(Object entity) {
>     audit( "preUpdate", entity);
>   }
>   public void postUpdate(Object entity) {
>     audit( "postUpdate", entity);
>   }
>   public void prePersist(Object entity) {
> 	    audit( "prePersist", entity);
> 	  }
> 	  public void postPersist(Object entity) {
> 	    audit( "postPersist", entity);
> 	  }
>   public void audit(String eventName, Object entity) {
>     if (entity instanceof IAudit) {
>       IAudit auditEntity = (IAudit) entity;
>       System.out.println("****T1EntityListener inst=" + instNum + ", event=" + eventName + ", entity=" + auditEntity + ", ver=" + auditEntity.getVerId());
>       try {
>         auditEntity.setModifyDateTime(createTimeStamp());
>       } catch (Exception e) {
>         throw new RuntimeException(e);
>       }
>     }
>   }
>   
>   private Timestamp createTimeStamp() {
> 	    return new Timestamp(System.currentTimeMillis());
> 	  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.