From 1768eee09f7dff04ad7c4d95c9ec459a25833b23 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sun, 31 Dec 2023 17:35:36 +0100 Subject: [PATCH] BAEL-6979: How to fix Hibernate QueryException: Named parameter not bound (#15451) --- .../persistentobject/HibernateUtil.java | 2 + .../namedparameternotbound/Person.java | 38 +++++++++++++ ...medParameterNotBoundExceptionUnitTest.java | 53 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/namedparameternotbound/Person.java create mode 100644 persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/namedparameternotbound/NamedParameterNotBoundExceptionUnitTest.java diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java index 641b80b412..bbf8f46e82 100644 --- a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/persistentobject/HibernateUtil.java @@ -11,6 +11,7 @@ import org.hibernate.service.ServiceRegistry; import com.baeldung.hibernate.exception.persistentobject.entity.Article; import com.baeldung.hibernate.exception.persistentobject.entity.Author; import com.baeldung.hibernate.exception.persistentobject.entity.Book; +import com.baeldung.hibernate.namedparameternotbound.Person; public class HibernateUtil { private static SessionFactory sessionFactory; @@ -34,6 +35,7 @@ public class HibernateUtil { configuration.addAnnotatedClass(Book.class); configuration.addAnnotatedClass(Author.class); configuration.addAnnotatedClass(Article.class); + configuration.addAnnotatedClass(Person.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/namedparameternotbound/Person.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/namedparameternotbound/Person.java new file mode 100644 index 0000000000..e5f5ebc4a0 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/namedparameternotbound/Person.java @@ -0,0 +1,38 @@ +package com.baeldung.hibernate.namedparameternotbound; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Person { + + @Id + private int id; + private String firstName; + private String lastName; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + +} diff --git a/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/namedparameternotbound/NamedParameterNotBoundExceptionUnitTest.java b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/namedparameternotbound/NamedParameterNotBoundExceptionUnitTest.java new file mode 100644 index 0000000000..f16a89917d --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/namedparameternotbound/NamedParameterNotBoundExceptionUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.hibernate.namedparameternotbound; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.hibernate.QueryException; +import org.hibernate.Session; +import org.hibernate.query.Query; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.baeldung.hibernate.exception.persistentobject.HibernateUtil; + +class NamedParameterNotBoundExceptionUnitTest { + + private static Session session; + + @BeforeAll + static void init() { + session = HibernateUtil.getSessionFactory() + .openSession(); + session.beginTransaction(); + } + + @AfterAll + static void clear() { + session.close(); + } + + @Test + void whenSettingValueToNamedParameter_thenDoNotThrowQueryException() { + Query query = session.createQuery("FROM Person p WHERE p.firstName = :firstName", Person.class); + query.setParameter("firstName", "Azhrioun"); + + assertNotNull(query.list()); + } + + @Test + void whenNotSettingValueToNamedParameter_thenThrowQueryException() { + Exception exception = assertThrows(QueryException.class, () -> { + Query query = session.createQuery("FROM Person p WHERE p.firstName = :firstName", Person.class); + query.list(); + }); + + String expectedMessage = "Named parameter not bound"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + +}