From c68ee78e45f596735e694050f23787651d30a5d6 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sun, 9 Oct 2022 21:18:50 +0200 Subject: [PATCH] BAEL-5697: Add new section in Hibernate Exceptions article (#12797) --- .../baeldung/hibernate/exception/Product.java | 8 ++- .../exception/HibernateExceptionUnitTest.java | 62 +++++++++---------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/persistence-modules/hibernate-enterprise/src/main/java/com/baeldung/hibernate/exception/Product.java b/persistence-modules/hibernate-enterprise/src/main/java/com/baeldung/hibernate/exception/Product.java index 031fa38de0..0724ced56b 100644 --- a/persistence-modules/hibernate-enterprise/src/main/java/com/baeldung/hibernate/exception/Product.java +++ b/persistence-modules/hibernate-enterprise/src/main/java/com/baeldung/hibernate/exception/Product.java @@ -3,12 +3,14 @@ package com.baeldung.hibernate.exception; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; +import javax.persistence.Table; @Entity +@Table(name = "PRODUCT") public class Product { private int id; - + private String name; private String description; @@ -20,8 +22,8 @@ public class Product { public void setId(int id) { this.id = id; } - - @Column(nullable=false) + + @Column(nullable = false) public String getName() { return name; } diff --git a/persistence-modules/hibernate-enterprise/src/test/java/com/baeldung/hibernate/exception/HibernateExceptionUnitTest.java b/persistence-modules/hibernate-enterprise/src/test/java/com/baeldung/hibernate/exception/HibernateExceptionUnitTest.java index 3581c81daa..679f786796 100644 --- a/persistence-modules/hibernate-enterprise/src/test/java/com/baeldung/hibernate/exception/HibernateExceptionUnitTest.java +++ b/persistence-modules/hibernate-enterprise/src/test/java/com/baeldung/hibernate/exception/HibernateExceptionUnitTest.java @@ -25,6 +25,7 @@ import org.hibernate.cfg.Configuration; import org.hibernate.exception.ConstraintViolationException; import org.hibernate.exception.DataException; import org.hibernate.exception.SQLGrammarException; +import org.hibernate.hql.internal.ast.QuerySyntaxException; import org.hibernate.query.NativeQuery; import org.hibernate.tool.schema.spi.CommandAcceptanceException; import org.hibernate.tool.schema.spi.SchemaManagementException; @@ -37,8 +38,7 @@ import org.slf4j.LoggerFactory; public class HibernateExceptionUnitTest { - private static final Logger logger = LoggerFactory - .getLogger(HibernateExceptionUnitTest.class); + private static final Logger logger = LoggerFactory.getLogger(HibernateExceptionUnitTest.class); private SessionFactory sessionFactory; @Before @@ -51,12 +51,10 @@ public class HibernateExceptionUnitTest { private Configuration getConfiguration() { Configuration cfg = new Configuration(); - cfg.setProperty(AvailableSettings.DIALECT, - "org.hibernate.dialect.H2Dialect"); + cfg.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect"); cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "none"); cfg.setProperty(AvailableSettings.DRIVER, "org.h2.Driver"); - cfg.setProperty(AvailableSettings.URL, - "jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1"); + cfg.setProperty(AvailableSettings.URL, "jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1"); cfg.setProperty(AvailableSettings.USER, "sa"); cfg.setProperty(AvailableSettings.PASS, ""); return cfg; @@ -68,8 +66,7 @@ public class HibernateExceptionUnitTest { thrown.expectMessage("Unknown entity: java.lang.String"); Session session = sessionFactory.openSession(); - NativeQuery query = session - .createNativeQuery("select name from PRODUCT", String.class); + NativeQuery query = session.createNativeQuery("select name from PRODUCT", String.class); query.getResultList(); } @@ -77,12 +74,21 @@ public class HibernateExceptionUnitTest { @SuppressWarnings("rawtypes") public void whenQueryExecuted_thenOK() { Session session = sessionFactory.openSession(); - NativeQuery query = session - .createNativeQuery("select name from PRODUCT"); + NativeQuery query = session.createNativeQuery("select name from PRODUCT"); List results = query.getResultList(); assertNotNull(results); } + @Test + public void whenQueryExecutedWithInvalidClassName_thenQuerySyntaxException() { + thrown.expectCause(isA(QuerySyntaxException.class)); + thrown.expectMessage("PRODUCT is not mapped [from PRODUCT]"); + + Session session = sessionFactory.openSession(); + List results = session.createQuery("from PRODUCT", Product.class) + .getResultList(); + } + @Test public void givenEntityWithoutId_whenSessionFactoryCreated_thenAnnotationException() { thrown.expect(AnnotationException.class); @@ -111,8 +117,7 @@ public class HibernateExceptionUnitTest { thrown.expectMessage("Halting on error : Error executing DDL"); Configuration cfg = getConfiguration(); - cfg.setProperty(AvailableSettings.DIALECT, - "org.hibernate.dialect.MySQLDialect"); + cfg.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.MySQLDialect"); cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "update"); // This does not work due to hibernate bug @@ -128,8 +133,7 @@ public class HibernateExceptionUnitTest { public void givenMissingTable_whenEntitySaved_thenSQLGrammarException() { thrown.expect(isA(PersistenceException.class)); thrown.expectCause(isA(SQLGrammarException.class)); - thrown - .expectMessage("SQLGrammarException: could not prepare statement"); + thrown.expectMessage("SQLGrammarException: could not prepare statement"); Configuration cfg = getConfiguration(); cfg.addAnnotatedClass(Product.class); @@ -159,12 +163,10 @@ public class HibernateExceptionUnitTest { public void givenMissingTable_whenQueryExecuted_thenSQLGrammarException() { thrown.expect(isA(PersistenceException.class)); thrown.expectCause(isA(SQLGrammarException.class)); - thrown - .expectMessage("SQLGrammarException: could not prepare statement"); + thrown.expectMessage("SQLGrammarException: could not prepare statement"); Session session = sessionFactory.openSession(); - NativeQuery query = session.createNativeQuery( - "select * from NON_EXISTING_TABLE", Product.class); + NativeQuery query = session.createNativeQuery("select * from NON_EXISTING_TABLE", Product.class); query.getResultList(); } @@ -172,8 +174,7 @@ public class HibernateExceptionUnitTest { public void whenDuplicateIdSaved_thenConstraintViolationException() { thrown.expect(isA(PersistenceException.class)); thrown.expectCause(isA(ConstraintViolationException.class)); - thrown.expectMessage( - "ConstraintViolationException: could not execute statement"); + thrown.expectMessage("ConstraintViolationException: could not execute statement"); Session session = null; Transaction transaction = null; @@ -199,8 +200,7 @@ public class HibernateExceptionUnitTest { @Test public void givenNotNullPropertyNotSet_whenEntityIdSaved_thenPropertyValueException() { thrown.expect(isA(PropertyValueException.class)); - thrown.expectMessage( - "not-null property references a null or transient value"); + thrown.expectMessage("not-null property references a null or transient value"); Session session = null; Transaction transaction = null; @@ -225,20 +225,17 @@ public class HibernateExceptionUnitTest { @Test public void givenQueryWithDataTypeMismatch_WhenQueryExecuted_thenDataException() { thrown.expectCause(isA(DataException.class)); - thrown.expectMessage( - "org.hibernate.exception.DataException: could not prepare statement"); + thrown.expectMessage("org.hibernate.exception.DataException: could not prepare statement"); Session session = sessionFactory.openSession(); - NativeQuery query = session.createNativeQuery( - "select * from PRODUCT where id='wrongTypeId'", Product.class); + NativeQuery query = session.createNativeQuery("select * from PRODUCT where id='wrongTypeId'", Product.class); query.getResultList(); } @Test public void givenSessionContainingAnId_whenIdAssociatedAgain_thenNonUniqueObjectException() { thrown.expect(isA(NonUniqueObjectException.class)); - thrown.expectMessage( - "A different object with the same identifier value was already associated with the session"); + thrown.expectMessage("A different object with the same identifier value was already associated with the session"); Session session = null; Transaction transaction = null; @@ -269,8 +266,7 @@ public class HibernateExceptionUnitTest { @Test public void whenDeletingADeletedObject_thenOptimisticLockException() { thrown.expect(isA(OptimisticLockException.class)); - thrown.expectMessage( - "Batch update returned unexpected row count from update"); + thrown.expectMessage("Batch update returned unexpected row count from update"); thrown.expectCause(isA(StaleStateException.class)); Session session = null; @@ -307,8 +303,7 @@ public class HibernateExceptionUnitTest { @Test public void whenUpdatingNonExistingObject_thenStaleStateException() { thrown.expect(isA(OptimisticLockException.class)); - thrown - .expectMessage("Row was updated or deleted by another transaction"); + thrown.expectMessage("Row was updated or deleted by another transaction"); thrown.expectCause(isA(StaleObjectStateException.class)); Session session = null; @@ -390,8 +385,7 @@ public class HibernateExceptionUnitTest { public void givenExistingEntity_whenIdUpdated_thenHibernateException() { thrown.expect(isA(PersistenceException.class)); thrown.expectCause(isA(HibernateException.class)); - thrown.expectMessage( - "identifier of an instance of com.baeldung.hibernate.exception.Product was altered"); + thrown.expectMessage("identifier of an instance of com.baeldung.hibernate.exception.Product was altered"); Session session = null; Transaction transaction = null;