From 6f8d130786ab5e0998d91ed9be8883d85358e0a6 Mon Sep 17 00:00:00 2001 From: eelhazati Date: Tue, 18 Dec 2018 12:33:49 +0100 Subject: [PATCH 1/2] Hibernate 5 bootstrap API Test. --- .../bootstrap/Hibernate5BootstrapAPITest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java new file mode 100644 index 0000000000..39111f6256 --- /dev/null +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java @@ -0,0 +1,69 @@ +package com.baeldung.hibernate.bootstrap; + +import com.baeldung.hibernate.pojo.Movie; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.BootstrapServiceRegistry; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class Hibernate5BootstrapAPITest { + + SessionFactory sessionFactory = null; + Session session = null; + + @Before + public void setUp() throws IOException { + + BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder() + .build(); + + ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder(bootstrapRegistry) + // No need for hibernate.cfg.xml file, an hibernate.properties is sufficient. + //.configure() + .build(); + + MetadataSources metadataSources = new MetadataSources(standardRegistry); + metadataSources.addAnnotatedClass(Movie.class); + + Metadata metadata = metadataSources.getMetadataBuilder().build(); + + sessionFactory = metadata.buildSessionFactory(); + } + + @Test + public void testBuildSessionFactory() { + assertNotNull(sessionFactory); + session = sessionFactory.openSession(); + assertNotNull(session); + + //Persist Movie + session.getTransaction().begin(); + Movie movie = new Movie(); + movie.setId(100L); + session.persist(movie); + session.getTransaction().commit(); + + List movies = session.createQuery("FROM Movie").list(); + assertNotNull(movies); + assertEquals(movies.size(), 1L); + } + + @After + public void clean() throws IOException { + session.close(); + sessionFactory.close(); + } +} From 5996de6af1a681d8c9a6c9c930a57b498daab92b Mon Sep 17 00:00:00 2001 From: "EZZEDDINE.ELHAZATI" Date: Wed, 19 Dec 2018 16:04:46 +0100 Subject: [PATCH 2/2] rename test class. --- ....java => BootstrapAPIIntegrationTest.java} | 30 +++---------------- 1 file changed, 4 insertions(+), 26 deletions(-) rename persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/{Hibernate5BootstrapAPITest.java => BootstrapAPIIntegrationTest.java} (62%) diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/BootstrapAPIIntegrationTest.java similarity index 62% rename from persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java rename to persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/BootstrapAPIIntegrationTest.java index 39111f6256..19988b45be 100644 --- a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/Hibernate5BootstrapAPITest.java +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/bootstrap/BootstrapAPIIntegrationTest.java @@ -1,7 +1,6 @@ package com.baeldung.hibernate.bootstrap; import com.baeldung.hibernate.pojo.Movie; -import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; @@ -10,22 +9,18 @@ import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; import org.junit.After; -import org.junit.Before; import org.junit.Test; import java.io.IOException; -import java.util.List; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -public class Hibernate5BootstrapAPITest { +public class BootstrapAPIIntegrationTest { SessionFactory sessionFactory = null; - Session session = null; - @Before - public void setUp() throws IOException { + @Test + public void whenServiceRegistryAndMetadata_thenSessionFactory() throws IOException { BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder() .build(); @@ -41,29 +36,12 @@ public class Hibernate5BootstrapAPITest { Metadata metadata = metadataSources.getMetadataBuilder().build(); sessionFactory = metadata.buildSessionFactory(); - } - - @Test - public void testBuildSessionFactory() { assertNotNull(sessionFactory); - session = sessionFactory.openSession(); - assertNotNull(session); - - //Persist Movie - session.getTransaction().begin(); - Movie movie = new Movie(); - movie.setId(100L); - session.persist(movie); - session.getTransaction().commit(); - - List movies = session.createQuery("FROM Movie").list(); - assertNotNull(movies); - assertEquals(movies.size(), 1L); + sessionFactory.close(); } @After public void clean() throws IOException { - session.close(); sessionFactory.close(); } }