-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-8172: Fixing hibernate QueryException
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ceptions/src/main/java/com/baeldung/hibernate/noargumentforordinalparameter/Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...eldung/hibernate/noargumentforordinalparameter/NoArgumentForOrdinalParameterUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |