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 bbf8f46e8292..95eabd191555 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 @@ -12,6 +12,7 @@ import com.baeldung.hibernate.exception.persistentobject.entity.Author; import com.baeldung.hibernate.exception.persistentobject.entity.Book; import com.baeldung.hibernate.namedparameternotbound.Person; +import com.baeldung.hibernate.noargumentforordinalparameter.Employee; public class HibernateUtil { private static SessionFactory sessionFactory; @@ -36,9 +37,10 @@ public static SessionFactory getSessionFactory() { configuration.addAnnotatedClass(Author.class); configuration.addAnnotatedClass(Article.class); configuration.addAnnotatedClass(Person.class); + configuration.addAnnotatedClass(Employee.class); - ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() - .applySettings(configuration.getProperties()).build(); + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()) + .build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { e.printStackTrace(); diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/noargumentforordinalparameter/Employee.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/noargumentforordinalparameter/Employee.java new file mode 100644 index 000000000000..dd94ada57b4d --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/noargumentforordinalparameter/Employee.java @@ -0,0 +1,37 @@ +package com.baeldung.hibernate.noargumentforordinalparameter; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Employee { + @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/noargumentforordinalparameter/NoArgumentForOrdinalParameterUnitTest.java b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/noargumentforordinalparameter/NoArgumentForOrdinalParameterUnitTest.java new file mode 100644 index 000000000000..a55332377ddc --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/test/java/com/baeldung/hibernate/noargumentforordinalparameter/NoArgumentForOrdinalParameterUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.hibernate.noargumentforordinalparameter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.hibernate.QueryParameterException; +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; +import com.baeldung.hibernate.noargumentforordinalparameter.Employee; + +class NoArgumentForOrdinalParameterUnitTest { + + private static Session session; + + @BeforeAll + static void init() { + session = HibernateUtil.getSessionFactory() + .openSession(); + session.beginTransaction(); + } + + @AfterAll + static void clear() { + session.close(); + } + + @Test + void whenMissingParameter_thenThrowQueryParameterException() { + assertThatThrownBy(() -> { + Query query = session.createQuery("FROM Employee emp WHERE firstName = ?1 AND lastName = ?2", Employee.class); + query.setParameter(1, "Jean"); + + query.list(); + }).isInstanceOf(QueryParameterException.class) + .hasMessageContaining("No argument for ordinal parameter"); + } + + @Test + void whenDefiningAllParameters_thenCorrect() { + Query query = session.createQuery("FROM Employee emp WHERE firstName = ?1 AND lastName = ?2", Employee.class); + query.setParameter(1, "Jean") + .setParameter(2, "Smith"); + + assertThat(query.list()).isNotNull(); + } + +}