BAEL-6979: How to fix Hibernate QueryException: Named parameter not bound (#15451)
This commit is contained in:
parent
324082f9a8
commit
1768eee09f
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Person> 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<Person> 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue