diff --git a/persistence-modules/hibernate-mapping-2/pom.xml b/persistence-modules/hibernate-mapping-2/pom.xml index 10c07c95eb..06a1535442 100644 --- a/persistence-modules/hibernate-mapping-2/pom.xml +++ b/persistence-modules/hibernate-mapping-2/pom.xml @@ -3,7 +3,7 @@ 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"> 4.0.0 - hibernate-mapping-2y + hibernate-mapping-2 0.1-SNAPSHOT hibernate-mapping-2 diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java index 19d1a5ff50..69b791b4d4 100644 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java @@ -45,6 +45,13 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest { projects.add(new Project("Networking Project")); session.persist(new Employee("Peter", "Oven", projects)); session.persist(new Employee("Allan", "Norman", projects)); + + Set employees = new HashSet(); + employees.add(new Employee("Sam", "Curran")); + employees.add(new Employee("Tom", "Curran")); + Project project = new Project("Database Project"); + project.setEmployees(employees); + session.persist(project); } } diff --git a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationMainIntegrationTest.java b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationMainIntegrationTest.java index bfa158d43f..71051c821c 100644 --- a/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationMainIntegrationTest.java +++ b/persistence-modules/hibernate-mapping-2/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationMainIntegrationTest.java @@ -1,7 +1,7 @@ package com.baeldung.hibernate.manytomany; -import static junit.framework.TestCase.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.HashSet; import java.util.List; @@ -23,60 +23,66 @@ import com.baeldung.manytomany.util.HibernateUtil; * Configured in: manytomany.cfg.xml */ public class HibernateManyToManyAnnotationMainIntegrationTest { - private static SessionFactory sessionFactory; + private static SessionFactory sessionFactory; - private Session session; + private Session session; - @BeforeClass - public static void beforeTests() { - sessionFactory = HibernateUtil.getSessionFactory(); - } + @BeforeClass + public static void beforeTests() { + sessionFactory = HibernateUtil.getSessionFactory(); + } - @Before - public void setUp() { - session = sessionFactory.openSession(); - session.beginTransaction(); - } + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } - @Test - public void givenData_whenInsert_thenCreatesMtoMrelationship() { - String[] employeeData = { "Peter Oven", "Allan Norman" }; - String[] projectData = { "IT Project", "Networking Project" }; - Set projects = new HashSet(); - - for (String proj : projectData) { - projects.add(new Project(proj)); - } - - for (String emp : employeeData) { - Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]); - assertEquals(0, employee.getProjects().size()); - employee.setProjects(projects); - session.persist(employee); - assertNotNull(employee); - } - } - - @Test + @Test public void givenSession_whenRead_thenReturnsMtoMdata() { + prepareData(); + @SuppressWarnings("unchecked") + List employeeList = session.createQuery("FROM Employee").list(); @SuppressWarnings("unchecked") - List employeeList = session.createQuery("FROM Employee").list(); + List projectList = session.createQuery("FROM Project").list(); assertNotNull(employeeList); + assertNotNull(projectList); + assertEquals(2, employeeList.size()); + assertEquals(2, projectList.size()); + for(Employee employee : employeeList) { assertNotNull(employee.getProjects()); } + for(Project project : projectList) { + assertNotNull(project.getEmployees()); + } } - @After - public void tearDown() { - session.getTransaction() - .commit(); - session.close(); - } + private void prepareData() { + String[] employeeData = { "Peter Oven", "Allan Norman" }; + String[] projectData = { "IT Project", "Networking Project" }; + Set projects = new HashSet(); - @AfterClass - public static void afterTests() { - sessionFactory.close(); - } + for (String proj : projectData) { + projects.add(new Project(proj)); + } + + for (String emp : employeeData) { + Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]); + employee.setProjects(projects); + session.persist(employee); + } + } + + @After + public void tearDown() { + session.getTransaction().commit(); + session.close(); + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); + } }