openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wanyna <wan...@hotmail.com>
Subject why not an EntityExistsException was thrown?
Date Wed, 04 Apr 2007 03:26:02 GMT

I was confused in result of my test cases.
My test is about JPA exceptions. I use derby in my tests.
The first case is simply persist an object, the PK of the object has existed
in database,
so exception arose.
			entityManager = factory.createEntityManager();
			entityManager.getTransaction().begin();
			...
			entityManager.persist(anObject);	
			entityManager.getTransaction().commit();
exception is:
<2|true|0.9.7-incubating-SNAPSHOT> org.apache.openjpa.util.StoreException:
The transaction has been rolled back.  See the nested exceptions for details
on the errors that occurred.
	at
org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2091)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1938)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1836)
	at
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1754)
	at
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:76)
	at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1311)
	at
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:863)
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:406)
	at test.Main.main(Main.java:82)
Caused by: <2|false|0.9.7-incubating-SNAPSHOT>
org.apache.openjpa.util.StoreException: The statement was aborted because it
would have caused a duplicate key value in a unique or primary key
constraint or unique index identified by 'SQL070403054930170' defined on
'BSC'. {prepstmnt 15774883 INSERT INTO BSC (objid, objname, created,
msc_name, objclass) VALUES (?, ?, ?, ?, ?) [params=(long) 1, (String)
objname1, (Timestamp) 2006-05-04 18:13:51.0, (String) objname0, (String)
bsc]} [code=-1, state=23505]
FailedObject: generate.Bsc@5d391d
	at
org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:3764)
	at
org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:94)
	at
org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:64)
	at
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:103)
	at
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:68)
	at
org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flushPrimaryRow(OperationOrderUpdateManager.java:200)
	at
org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flush(OperationOrderUpdateManager.java:86)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:86)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:69)
	at
org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:511)
	at
org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:127)
	... 8 more
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: The statement
was aborted because it would have caused a duplicate key value in a unique
or primary key constraint or unique index identified by 'SQL070403054930170'
defined on 'BSC'. {prepstmnt 15774883 INSERT INTO BSC (objid, objname,
created, msc_name, objclass) VALUES (?, ?, ?, ?, ?) [params=(long) 1,
(String) objname1, (Timestamp) 2006-05-04 18:13:51.0, (String) objname0,
(String) bsc]} [code=-1, state=23505]
	at
org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:188)
	at
org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$800(LoggingConnectionDecorator.java:53)
	at
org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:854)
	at
org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:266)
	at
org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1360)
	at
org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:92)
	... 15 more

I wonder why not an EntityExistsException was thrown?

In the second case, I add a query before persist,  
			entityManager = factory.createEntityManager();
			/*
			 * query before persist, even bofore transaction effects samely.
			 * this query makes exception thrown in persist section difference.
			 * it seems that query what entity is not important,
			 * but must get result and result must has some record.
			 */
			entityManager.createQuery(jpql).getResultList();

			entityManager.getTransaction().begin();
			...
			entityManager.persist(anObject);	
			entityManager.getTransaction().commit();
exception:
<2|false|0.9.7-incubating-SNAPSHOT>
org.apache.openjpa.persistence.EntityExistsException: An object of type
"generate.Bsc" with oid "generate.Bsc-1:objname1" already exists in this
context; another cannot be persisted.
FailedObject: generate.Bsc@14b6bed
	at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2370)
	at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2206)
	at
org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:991)
	at
org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:526)
	at test.Main.main(Main.java:72)
FailedObject:generate.Bsc@14b6bed

An EntityExistsException was thrown this time, why? 

Exception is different, and when exception was thrown is also different.
first case exception was caught at commit,
second case, exception was caught at persist, why?

-- 
View this message in context: http://www.nabble.com/why-not-an-EntityExistsException-was-thrown--tf3523673.html#a9830350
Sent from the open-jpa-dev mailing list archive at Nabble.com.


Mime
View raw message