From hibernate-commits at lists.jboss.org Wed Jun 25 06:07:05 2008 Content-Type: multipart/mixed; boundary="===============7449868111343894495==" MIME-Version: 1.0 From: hibernate-commits at lists.jboss.org To: hibernate-commits at lists.jboss.org Subject: [hibernate-commits] Hibernate SVN: r14812 - in entitymanager/trunk/src: test/org/hibernate/ejb/test/exception and 1 other directory. Date: Wed, 25 Jun 2008 06:07:04 -0400 Message-ID: --===============7449868111343894495== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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.j= ava Modified: entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImpl= .java entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/ExceptionT= est.java entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.java Log: EJB-349 - Removed the explcit catching of the ConstraintViolationException. This me= ans that a ConstraintViolationException gets now wrapped around a Persisten= ceException. We still have to wrap the exception since we don't want to thr= ow any Hibernate specific exceptions out of the entity managet. Removing th= e explicit catch of ConstraintViolationException also ment that EntityExist= sException 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/AbstractEntityMana= gerImpl.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImp= l.java 2008-06-25 07:32:47 UTC (rev 14811) +++ entitymanager/trunk/src/java/org/hibernate/ejb/AbstractEntityManagerImp= l.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 Gavin King * @author Emmanuel Bernard */ +(a)SuppressWarnings("unchecked") public abstract class AbstractEntityManagerImpl implements HibernateEntity= ManagerImplementor, Serializable { private static final Logger log =3D LoggerFactory.getLogger( AbstractEnti= tyManagerImpl.class ); = @@ -398,25 +398,6 @@ } } = - /** - * adjust the flush mode to match the no tx / no flush behavior - */ - //remove - private void adjustFlushMode() { - Session session =3D getSession(); - - boolean isTransactionActive =3D isTransactionInProgress(); - - if ( isTransactionActive && session.getFlushMode() =3D=3D FlushMode.MANU= AL ) { - log.debug( "Transaction activated, move to FlushMode {}", flushModeType= ); - setFlushMode( flushModeType ); - } - else if ( ! isTransactionActive && session.getFlushMode() !=3D 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 =3D wrapStaleStateException( (StaleStateExcepti= on) 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/Abstrac= tEntityManagerImpl.java ___________________________________________________________________ Name: svn:keywords - Author Date Id Revision + Id Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Exc= eptionTest.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Exception= Test.java 2008-06-25 07:32:47 UTC (rev 14811) +++ entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Exception= Test.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 */ +(a)SuppressWarnings("unchecked") public class ExceptionTest extends TestCase { + = + private final Logger log =3D LoggerFactory.getLogger(ExceptionTest.class); = public void testOptimisticLockingException() throws Exception { EntityManager em =3D factory.createEntityManager(); @@ -48,7 +56,7 @@ em.close(); } } - + = public void testEntityNotFoundException() throws Exception { EntityManager em =3D factory.createEntityManager( ); Music music =3D em.getReference( Music.class, new Integer(-1) ); @@ -57,12 +65,39 @@ fail("Non existent entity should raise an exception when state is acces= sed"); } catch( EntityNotFoundException e ) { - //success + log.debug("success"); } finally { em.close(); } } + = + public void testConstraintViolationException() throws Exception { + EntityManager em =3D factory.createEntityManager( ); + em.getTransaction().begin(); + Music music =3D new Music(); + music.setName( "Jazz" ); + em.persist( music ); + Musician lui =3D new Musician(); + lui.setName("Lui Armstrong"); + lui.setFavouriteMusic(music); + em.persist(lui); + em.getTransaction().commit(); + try { + em.getTransaction().begin(); + String hqlDelete =3D "delete Music where name =3D :name"; + em.createQuery( hqlDelete ).setParameter( "name", "Jazz" ).executeUpdat= e(); + em.getTransaction().commit(); + fail(); + } + catch( PersistenceException e ) { + Throwable t =3D e.getCause(); + assertTrue("Should be a constraint violation", t instanceof ConstraintV= iolationException); + } + 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/ex= ception/ExceptionTest.java ___________________________________________________________________ Name: svn:keywords + Id Modified: entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Mus= ic.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.jav= a 2008-06-25 07:32:47 UTC (rev 14811) +++ entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Music.jav= a 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 +(a)SuppressWarnings("serial") public class Music implements Serializable { private Integer id; private Integer version; @@ -32,7 +33,8 @@ this.name =3D name; } = - @Version public Integer getVersion() { + @Version = + public Integer getVersion() { return version; } = Property changes on: entitymanager/trunk/src/test/org/hibernate/ejb/test/ex= ception/Music.java ___________________________________________________________________ Name: svn:keywords + Id Added: entitymanager/trunk/src/test/org/hibernate/ejb/test/exception/Musici= an.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 + */ +(a)Entity +(a)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 =3D id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name =3D name; + } + + @ManyToOne + public Music getFavouriteMusic() { + return favouriteMusic; + } + + public void setFavouriteMusic(Music favouriteMusic) { + this.favouriteMusic =3D favouriteMusic; + } +} Property changes on: entitymanager/trunk/src/test/org/hibernate/ejb/test/ex= ception/Musician.java ___________________________________________________________________ Name: svn:keywords + Id Name: svn:eol-style + native --===============7449868111343894495==--