From 9baee5bc6e88df25304a9b10b2f66331c5fe108f Mon Sep 17 00:00:00 2001 From: Mike Baranski Date: Thu, 19 Sep 2019 23:53:16 -0400 Subject: [PATCH] [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 * Update persistence-modules/hibernate-parameters/pom.xml Co-Authored-By: KevinGilmore * Update persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java Co-Authored-By: KevinGilmore * Update persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java Co-Authored-By: KevinGilmore * Set version and add jar packaging --- .../hibernate-parameters/pom.xml | 52 ++++++++++++++ .../hibernate_parameters/Event.hbm.xml | 15 ++++ .../baeldung/hibernate_parameters/Event.java | 30 ++++++++ .../NamedParameterUnitTest.java | 72 +++++++++++++++++++ .../src/test/resources/hibernate.cfg.xml | 28 ++++++++ persistence-modules/pom.xml | 1 + 6 files changed, 198 insertions(+) create mode 100644 persistence-modules/hibernate-parameters/pom.xml create mode 100644 persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.hbm.xml create mode 100644 persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.java create mode 100644 persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java create mode 100644 persistence-modules/hibernate-parameters/src/test/resources/hibernate.cfg.xml diff --git a/persistence-modules/hibernate-parameters/pom.xml b/persistence-modules/hibernate-parameters/pom.xml new file mode 100644 index 0000000000..b744e181da --- /dev/null +++ b/persistence-modules/hibernate-parameters/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + com.baeldung + hibernate-parameters + 0.1-SNAPSHOT + hibernate-parameters + jar + Hibernate tutorial illustrating the use of named parameters + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.hibernate + hibernate-core + 5.4.4.Final + + + + com.h2database + h2 + 1.4.196 + + + + + + + false + src/test/java + + **/*.xml + + + + src/test/resources + + + + + + true + + + diff --git a/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.hbm.xml b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.hbm.xml new file mode 100644 index 0000000000..4055718776 --- /dev/null +++ b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.hbm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + diff --git a/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.java b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.java new file mode 100644 index 0000000000..f44b1bfd8a --- /dev/null +++ b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/Event.java @@ -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; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java new file mode 100644 index 0000000000..23854dc393 --- /dev/null +++ b/persistence-modules/hibernate-parameters/src/test/java/com/baeldung/hibernate_parameters/NamedParameterUnitTest.java @@ -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 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 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(); + } + } +} diff --git a/persistence-modules/hibernate-parameters/src/test/resources/hibernate.cfg.xml b/persistence-modules/hibernate-parameters/src/test/resources/hibernate.cfg.xml new file mode 100644 index 0000000000..480baae4c1 --- /dev/null +++ b/persistence-modules/hibernate-parameters/src/test/resources/hibernate.cfg.xml @@ -0,0 +1,28 @@ + + + + + + + org.h2.Driver + jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE + sa + + + 1 + + org.hibernate.dialect.H2Dialect + + org.hibernate.cache.internal.NoCacheProvider + + true + + create + + + + + + \ No newline at end of file diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 0c22267192..932b62a235 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -23,6 +23,7 @@ hibernate5 hibernate-ogm hibernate-mapping + hibernate-parameters influxdb java-cassandra java-cockroachdb