[BAEL-3058] Demonstrate named query (#7772)
* [BAEL-3058] Demonstrate named query * [BAEL-3058] Demonstrate the wrong way to do it, change to Junit 4 style test with annotations * Fix tab -> space and indentation * Moved hibernate-parameters to persistence-modules directory per request * Update persistence-modules/hibernate-parameters/pom.xml Co-Authored-By: KevinGilmore <kpg102@gmail.com> * Update persistence-modules/hibernate-parameters/pom.xml Co-Authored-By: KevinGilmore <kpg102@gmail.com> * Update persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java Co-Authored-By: KevinGilmore <kpg102@gmail.com> * Update persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java Co-Authored-By: KevinGilmore <kpg102@gmail.com> * Set version and add jar packaging
This commit is contained in:
parent
79ce3c0fdc
commit
9baee5bc6e
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>hibernate-parameters</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>hibernate-parameters</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Hibernate tutorial illustrating the use of named parameters</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>5.4.4.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.196</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<filtering>false</filtering>
|
||||
<directory>src/test/java</directory>
|
||||
<includes>
|
||||
<include>**/*.xml</include>
|
||||
</includes>
|
||||
</testResource>
|
||||
<testResource>
|
||||
<directory>src/test/resources</directory>
|
||||
</testResource>
|
||||
</testResources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="com.baeldung.hibernate_parameters">
|
||||
|
||||
<class name="Event" table="EVENTS">
|
||||
<id name="id" column="EVENT_ID">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="title"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.hibernate_parameters;
|
||||
|
||||
public class Event {
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
public Event() {
|
||||
}
|
||||
|
||||
public Event(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
private void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.hibernate_parameters;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.query.Query;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class NamedParameterUnitTest {
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
|
||||
.configure()
|
||||
.build();
|
||||
try {
|
||||
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.save(new Event("Event 1"));
|
||||
session.save(new Event("Event 2"));
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
} catch (Exception e) {
|
||||
StandardServiceRegistryBuilder.destroy(registry);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (sessionFactory != null) {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNamedParameterProvided_thenCorrect() {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Query<Event> query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class);
|
||||
|
||||
// This binds the value "Event1" to the parameter :eventTitle
|
||||
query.setParameter("eventTitle", "Event 1");
|
||||
|
||||
assertEquals(1, query.list().size());
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test(expected = org.hibernate.QueryException.class)
|
||||
public void whenNamedParameterMissing_thenThrowsQueryException() {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Query<Event> query = session.createQuery("from Event E WHERE E.title = :eventTitle", Event.class);
|
||||
|
||||
try {
|
||||
query.list();
|
||||
fail("We are expecting an exception!");
|
||||
} finally {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
<hibernate-configuration>
|
||||
|
||||
<session-factory>
|
||||
<property name="connection.driver_class">org.h2.Driver</property>
|
||||
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
|
||||
<property name="connection.username">sa</property>
|
||||
<property name="connection.password"/>
|
||||
|
||||
<property name="connection.pool_size">1</property>
|
||||
|
||||
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
|
||||
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
|
||||
|
||||
<property name="show_sql">true</property>
|
||||
|
||||
<property name="hbm2ddl.auto">create</property>
|
||||
|
||||
<mapping resource="com/baeldung/hibernate_parameters/Event.hbm.xml"/>
|
||||
|
||||
</session-factory>
|
||||
|
||||
</hibernate-configuration>
|
|
@ -23,6 +23,7 @@
|
|||
<module>hibernate5</module>
|
||||
<module>hibernate-ogm</module>
|
||||
<module>hibernate-mapping</module>
|
||||
<module>hibernate-parameters</module>
|
||||
<module>influxdb</module>
|
||||
<module>java-cassandra</module>
|
||||
<module>java-cockroachdb</module>
|
||||
|
|
Loading…
Reference in New Issue