Skip to content

Commit

Permalink
BAEL-8172: Fixing hibernate QueryException
Browse files Browse the repository at this point in the history
  • Loading branch information
azhwani committed Dec 14, 2024
1 parent f569901 commit 0198c91
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Employee> 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<Employee> 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();
}

}

0 comments on commit 0198c91

Please sign in to comment.