diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java index 26daa5688e..e78d850c24 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java +++ b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/ExceptionExpectations.java @@ -7,6 +7,7 @@ package org.hibernate.test.exceptionhandling; import java.sql.SQLException; +import javax.persistence.NoResultException; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; import javax.persistence.RollbackException; @@ -54,6 +55,16 @@ interface ExceptionExpectations { assertThat( e.getCause(), instanceOf( QuerySyntaxException.class ) ); } + @Override + public void onGetSingleResultWithMultipleResults(RuntimeException e) { + assertThat( e, instanceOf( javax.persistence.NonUniqueResultException.class ) ); + } + + @Override + public void onGetSingleResultWithNoResults(RuntimeException e) { + assertThat( e, instanceOf( NoResultException.class ) ); + } + @Override public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) { assertThat( e, instanceOf( OptimisticLockException.class ) ); @@ -115,6 +126,16 @@ interface ExceptionExpectations { assertThat( e, instanceOf( QuerySyntaxException.class ) ); } + @Override + public void onGetSingleResultWithMultipleResults(RuntimeException e) { + assertThat( e, instanceOf( org.hibernate.NonUniqueResultException.class ) ); + } + + @Override + public void onGetSingleResultWithNoResults(RuntimeException e) { + assertThat( e, instanceOf( NoResultException.class ) ); + } + @Override public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) { assertThat( e, instanceOf( StaleObjectStateException.class ) ); @@ -174,6 +195,16 @@ interface ExceptionExpectations { assertThat( e.getCause(), instanceOf( QuerySyntaxException.class ) ); } + @Override + public void onGetSingleResultWithMultipleResults(RuntimeException e) { + assertThat( e, instanceOf( javax.persistence.NonUniqueResultException.class ) ); + } + + @Override + public void onGetSingleResultWithNoResults(RuntimeException e) { + assertThat( e, instanceOf( NoResultException.class ) ); + } + @Override public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) { assertThat( e, instanceOf( OptimisticLockException.class ) ); @@ -215,6 +246,10 @@ interface ExceptionExpectations { void onInvalidQueryExecuted(RuntimeException e); + void onGetSingleResultWithMultipleResults(RuntimeException e); + + void onGetSingleResultWithNoResults(RuntimeException e); + void onStaleObjectMergeAndUpdateFlush(RuntimeException e); void onIdentifierGeneratorFailure(RuntimeException e); diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java new file mode 100644 index 0000000000..ee38985c15 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QueryExceptionHandlingTest.java @@ -0,0 +1,99 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.exceptionhandling; + +import static org.junit.Assert.fail; + +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.transaction.TransactionUtil2; +import org.junit.Before; +import org.junit.Test; + +@RequiresDialect(H2Dialect.class) +public class QueryExceptionHandlingTest extends BaseExceptionHandlingTest { + + public QueryExceptionHandlingTest( + BootstrapMethod bootstrapMethod, + ExceptionHandlingSetting exceptionHandlingSetting, + ExceptionExpectations exceptionExpectations) { + super( bootstrapMethod, exceptionHandlingSetting, exceptionExpectations ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + A.class + }; + } + + @Before + public void initData() { + TransactionUtil2.inTransaction( sessionFactory(), s -> { + s.createQuery( "delete from A" ).executeUpdate(); + A a1 = new A(); + a1.id = 1; + s.persist( a1 ); + A a2 = new A(); + a2.id = 2; + s.persist( a2 ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-12666") + public void testInvalidQuery() { + try { + TransactionUtil2.inSession( sessionFactory(), s -> { + s.createQuery( "from A where blahblahblah" ).list(); + } ); + fail( "should have thrown an exception" ); + } + catch (RuntimeException expected) { + exceptionExpectations.onInvalidQueryExecuted( expected ); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-13300") + public void testGetSingleResultWithMultipleResults() { + try { + TransactionUtil2.inSession( sessionFactory(), s -> { + s.createQuery( "from A where id in (1, 2)" ).getSingleResult(); + } ); + fail( "should have thrown an exception" ); + } + catch (RuntimeException expected) { + exceptionExpectations.onGetSingleResultWithMultipleResults( expected ); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-13300") + public void testGetSingleResultWithNoResults() { + try { + TransactionUtil2.inSession( sessionFactory(), s -> { + s.createQuery( "from A where id = 3" ).getSingleResult(); + } ); + fail( "should have thrown an exception" ); + } + catch (RuntimeException expected) { + exceptionExpectations.onGetSingleResultWithNoResults( expected ); + } + } + + @Entity(name = "A") + public static class A { + @Id + private long id; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QuerySyntaxExceptionHandlingTest.java b/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QuerySyntaxExceptionHandlingTest.java deleted file mode 100644 index 6f3dca8679..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/exceptionhandling/QuerySyntaxExceptionHandlingTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.exceptionhandling; - -import static org.junit.Assert.fail; - -import javax.persistence.Entity; -import javax.persistence.Id; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.dialect.H2Dialect; -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -@TestForIssue(jiraKey = "HHH-12666") -@RequiresDialect(H2Dialect.class) -public class QuerySyntaxExceptionHandlingTest extends BaseExceptionHandlingTest { - - public QuerySyntaxExceptionHandlingTest( - BootstrapMethod bootstrapMethod, - ExceptionHandlingSetting exceptionHandlingSetting, - ExceptionExpectations exceptionExpectations) { - super( bootstrapMethod, exceptionHandlingSetting, exceptionExpectations ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - A.class - }; - } - - @Test - public void testInvalidQuery() { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - A a = new A(); - a.id = 1; - s.persist( a ); - s.flush(); - s.clear(); - - try { - s.createQuery( "from A where blahblahblah" ).list(); - fail( "should have thrown an exception" ); - } - catch (RuntimeException expected) { - exceptionExpectations.onInvalidQueryExecuted( expected ); - } - finally { - tx.rollback(); - s.close(); - } - } - - @Entity(name = "A") - public static class A { - @Id - private long id; - } -}