这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions persistence-modules/hibernate-exceptions-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Relevant Articles:
33 changes: 33 additions & 0 deletions persistence-modules/hibernate-exceptions-2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>hibernate-exceptions-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hibernate-exceptions-2</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>

<properties>
<h2.version>2.3.232</h2.version>
</properties>

</project>
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,42 @@
package com.baeldung.hibernate.noargumentforordinalparameter;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Map<String, Object> settings = new HashMap<>();
settings.put("hibernate.connection.driver_class", "org.h2.Driver");
settings.put("hibernate.connection.url", "jdbc:h2:mem:test");
settings.put("hibernate.connection.username", "sa");
settings.put("hibernate.connection.password", "");
settings.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
settings.put("hibernate.show_sql", "true");
settings.put("hibernate.hbm2ddl.auto", "update");

try {
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().applySettings(settings)
.build();

Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClass(Employee.class)
.getMetadataBuilder()
.build();

sessionFactory = metadata.getSessionFactoryBuilder()
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.baeldung.hibernate.noargumentforordinalparameter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

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;

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(() -> {
String selectQuery = """
FROM Employee
WHERE firstName = ?1
AND lastName = ?2
""";
Query<Employee> query = session.createQuery(selectQuery, Employee.class);
query.setParameter(1, "Jean");

query.list();
}).isInstanceOf(QueryException.class)
.hasMessageContaining("No argument for ordinal parameter");
}

@Test
void whenDefiningAllParameters_thenCorrect() {
String selectQuery = """
FROM Employee
WHERE firstName = ?1
AND lastName = ?2
""";
Query<Employee> query = session.createQuery(selectQuery, Employee.class);
query.setParameter(1, "Jean")
.setParameter(2, "Smith");

assertThat(query.list()).isNotNull();
}

}
1 change: 1 addition & 0 deletions persistence-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>hibernate-mapping-2</module>
<module>hibernate-annotations</module>
<module>hibernate-exceptions</module>
<module>hibernate-exceptions-2</module>
<module>hibernate-libraries</module>
<module>hibernate-jpa</module>
<module>hibernate-jpa-2</module>
Expand Down