Author: hardy.ferentschik
Date: 2008-06-25 06:07:04 -0400 (Wed, 25 Jun 2008)
New Revision: 14812
Added:
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musician.java
Modified:
entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java
Log:
EJB-349
- Removed the explcit catching of the ConstraintViolationException. This means that a
ConstraintViolationException gets now wrapped around a PersistenceException. We still have
to wrap the exception since we don't want to throw any Hibernate specific exceptions
out of the entity managet. Removing the explicit catch of ConstraintViolationException
also ment that EntityExistsException will never be thrown. This should be ok since the
specs only say "may be thrown".
- Added some tests
Modified: entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
===================================================================
---
entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2008-06-25
07:32:47 UTC (rev 14811)
+++
entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java 2008-06-25
10:07:04 UTC (rev 14812)
@@ -1,3 +1,4 @@
+// $Id:$
/*
* JBoss, the OpenSource EJB server Distributable under LGPL license. See terms of
license at
*
gnu.org.
@@ -9,7 +10,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
-import javax.persistence.EntityExistsException;
+
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
@@ -34,26 +35,24 @@
import org.hibernate.MappingException;
import org.hibernate.ObjectDeletedException;
import org.hibernate.ObjectNotFoundException;
+import org.hibernate.QueryException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
+import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.Transaction;
-import org.hibernate.UnresolvableObjectException;
-import org.hibernate.TypeMismatchException;
-import org.hibernate.QueryException;
import org.hibernate.TransientObjectException;
-import org.hibernate.StaleObjectStateException;
-import org.hibernate.annotations.common.util.ReflectHelper;
+import org.hibernate.TypeMismatchException;
+import org.hibernate.UnresolvableObjectException;
import org.hibernate.cfg.Environment;
import org.hibernate.ejb.transaction.JoinableCMTTransaction;
import org.hibernate.ejb.util.ConfigurationHelper;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
-import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.transaction.TransactionFactory;
-import org.hibernate.util.JTAHelper;
import org.hibernate.util.CollectionHelper;
+import org.hibernate.util.JTAHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,6 +60,7 @@
* @author <a href="mailto:gavin@hibernate.org">Gavin King</a>
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public abstract class AbstractEntityManagerImpl implements
HibernateEntityManagerImplementor, Serializable {
private static final Logger log = LoggerFactory.getLogger(
AbstractEntityManagerImpl.class );
@@ -398,25 +398,6 @@
}
}
- /**
- * adjust the flush mode to match the no tx / no flush behavior
- */
- //remove
- private void adjustFlushMode() {
- Session session = getSession();
-
- boolean isTransactionActive = isTransactionInProgress();
-
- if ( isTransactionActive && session.getFlushMode() == FlushMode.MANUAL ) {
- log.debug( "Transaction activated, move to FlushMode {}", flushModeType );
- setFlushMode( flushModeType );
- }
- else if ( ! isTransactionActive && session.getFlushMode() != FlushMode.MANUAL )
{
- log.debug( "Transaction not active, move to FlushMode NEVER" );
- session.setFlushMode( FlushMode.MANUAL );
- }
- }
-
public boolean isTransactionInProgress() {
return ( (SessionImplementor) getRawSession() ).isTransactionInProgress();
}
@@ -607,10 +588,6 @@
PersistenceException pe = wrapStaleStateException( (StaleStateException) e );
throwPersistenceException( pe );
}
- else if ( e instanceof ConstraintViolationException ) {
- //FIXME this is bad cause ConstraintViolationException happens in other circumstances
- throwPersistenceException( new EntityExistsException( e ) );
- }
else if ( e instanceof ObjectNotFoundException ) {
throwPersistenceException( new EntityNotFoundException( e.getMessage() ) );
}
Property changes on:
entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl.java
___________________________________________________________________
Name: svn:keywords
- Author Date Id Revision
+ Id
Modified:
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java
===================================================================
---
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java 2008-06-25
07:32:47 UTC (rev 14811)
+++
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java 2008-06-25
10:07:04 UTC (rev 14812)
@@ -1,18 +1,26 @@
-//$Id: $
+// $Id:$
package org.hibernate.ejb.test.exception;
import java.util.Map;
+
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.OptimisticLockException;
+import javax.persistence.PersistenceException;
import org.hibernate.cfg.Environment;
import org.hibernate.ejb.test.TestCase;
+import org.hibernate.exception.ConstraintViolationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public class ExceptionTest extends TestCase {
+
+ private final Logger log = LoggerFactory.getLogger(ExceptionTest.class);
public void testOptimisticLockingException() throws Exception {
EntityManager em = factory.createEntityManager();
@@ -48,7 +56,7 @@
em.close();
}
}
-
+
public void testEntityNotFoundException() throws Exception {
EntityManager em = factory.createEntityManager( );
Music music = em.getReference( Music.class, new Integer(-1) );
@@ -57,12 +65,39 @@
fail("Non existent entity should raise an exception when state is
accessed");
}
catch( EntityNotFoundException e ) {
- //success
+ log.debug("success");
}
finally {
em.close();
}
}
+
+ public void testConstraintViolationException() throws Exception {
+ EntityManager em = factory.createEntityManager( );
+ em.getTransaction().begin();
+ Music music = new Music();
+ music.setName( "Jazz" );
+ em.persist( music );
+ Musician lui = new Musician();
+ lui.setName("Lui Armstrong");
+ lui.setFavouriteMusic(music);
+ em.persist(lui);
+ em.getTransaction().commit();
+ try {
+ em.getTransaction().begin();
+ String hqlDelete = "delete Music where name = :name";
+ em.createQuery( hqlDelete ).setParameter( "name", "Jazz"
).executeUpdate();
+ em.getTransaction().commit();
+ fail();
+ }
+ catch( PersistenceException e ) {
+ Throwable t = e.getCause();
+ assertTrue("Should be a constraint violation", t instanceof
ConstraintViolationException);
+ }
+ finally {
+ em.close();
+ }
+ }
@Override
public Map getConfig() {
@@ -73,7 +108,7 @@
public Class[] getAnnotatedClasses() {
return new Class[] {
- Music.class
+ Music.class, Musician.class
};
}
}
Property changes on:
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java 2008-06-25
07:32:47 UTC (rev 14811)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java 2008-06-25
10:07:04 UTC (rev 14812)
@@ -1,4 +1,4 @@
-//$Id: $
+// $Id:$
package org.hibernate.ejb.test.exception;
import java.io.Serializable;
@@ -11,6 +11,7 @@
* @author Emmanuel Bernard
*/
@Entity
+@SuppressWarnings("serial")
public class Music implements Serializable {
private Integer id;
private Integer version;
@@ -32,7 +33,8 @@
this.name = name;
}
- @Version public Integer getVersion() {
+ @Version
+ public Integer getVersion() {
return version;
}
Property changes on:
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musician.java
===================================================================
--- entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musician.java
(rev 0)
+++ entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musician.java 2008-06-25
10:07:04 UTC (rev 14812)
@@ -0,0 +1,48 @@
+// $Id:$
+package org.hibernate.ejb.test.exception;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Entity
+@SuppressWarnings("serial")
+public class Musician implements Serializable {
+ private Integer id;
+
+ private String name;
+
+ private Music favouriteMusic;
+
+ @Id @GeneratedValue
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @ManyToOne
+ public Music getFavouriteMusic() {
+ return favouriteMusic;
+ }
+
+ public void setFavouriteMusic(Music favouriteMusic) {
+ this.favouriteMusic = favouriteMusic;
+ }
+}
Property changes on:
entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musician.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native