diff --git a/spring-hibernate4/src/main/resources/hibernate-annotation.cfg.xml b/spring-hibernate4/src/main/resources/hibernate-annotation.cfg.xml
index 92e323460d..311300c77c 100644
--- a/spring-hibernate4/src/main/resources/hibernate-annotation.cfg.xml
+++ b/spring-hibernate4/src/main/resources/hibernate-annotation.cfg.xml
@@ -5,15 +5,15 @@
com.mysql.jdbc.Driver
- bastard5
- jdbc:mysql://localhost:3306/setup
- root
+ mypassword
+ jdbc:mysql://localhost:3306/spring_hibernate_one_to_many?createDatabaseIfNotExist=true
+ myuser
org.hibernate.dialect.MySQLDialect
thread
true
-
-
+
+
diff --git a/spring-hibernate4/src/main/resources/one_to_many.sql b/spring-hibernate4/src/main/resources/one_to_many.sql
index f2da0b4429..7887ff7d9c 100644
--- a/spring-hibernate4/src/main/resources/one_to_many.sql
+++ b/spring-hibernate4/src/main/resources/one_to_many.sql
@@ -14,4 +14,4 @@ CREATE TABLE `Items` (
PRIMARY KEY (`id`),
KEY `cart_id` (`cart_id`),
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)
-) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
\ No newline at end of file
+) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtilTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtilTest.java
deleted file mode 100644
index 4d1b59a21e..0000000000
--- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtilTest.java
+++ /dev/null
@@ -1,43 +0,0 @@
-
-package com.baeldung.hibernate.oneToMany.config;
-
-import org.hibernate.SessionFactory;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-
-public class HibernateAnnotationUtilTest {
-
- public HibernateAnnotationUtilTest() {
- }
-
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
- @Test
- public void testGetSessionFactory() {
- System.out.println("getSessionFactory");
- SessionFactory expResult = null;
- SessionFactory result = HibernateAnnotationUtil.getSessionFactory();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
-}
diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
index 7ba6d3ade9..539298ae1e 100644
--- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
+++ b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java
@@ -1,8 +1,19 @@
package com.baeldung.hibernate.oneToMany.main;
-import org.junit.After;
-import org.junit.AfterClass;
+
+import com.baeldung.hibernate.oneToMany.model.Cart;
+import com.baeldung.hibernate.oneToMany.model.Items;
+import java.util.HashSet;
+import java.util.Set;
+import static org.hamcrest.Matchers.hasSize;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.dialect.HSQLDialect;
+import org.hibernate.service.ServiceRegistry;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -11,30 +22,68 @@ import static org.junit.Assert.*;
public class HibernateOneToManyAnnotationMainTest {
+ private static SessionFactory sessionFactory;
+
+ private Session session;
+
public HibernateOneToManyAnnotationMainTest() {
}
- @BeforeClass
- public static void setUpClass() {
- }
- @AfterClass
- public static void tearDownClass() {
+ @BeforeClass
+ public static void beforeTests() {
+ Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
+ .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
+ ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
+ sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Before
public void setUp() {
+ session = sessionFactory.openSession();
+ session.beginTransaction();
+
}
- @After
- public void tearDown() {
- }
+
+
@Test
- public void testMain() {
- System.out.println("main");
- String[] args = null;
- HibernateOneToManyAnnotationMain.main(args);
- fail("The test failed.");
+ public void testAddItemsToCart() {
+ Cart cart = new Cart();
+ Set cartItems = new HashSet<>();
+ cartItems = cart.getItems();
+ Assert.assertNull(cartItems);
+ Items item1 = new Items("I10", 10, 1, cart);
+ assertNotNull(item1);
+ Set itemsSet = new HashSet();
+ cart.setItems(itemsSet);
+ assertNotNull(cart);
+ System.out.println("Items added to cart");
+
}
+ @Test
+ public void testSaveCart(){
+ Cart cart = new Cart();
+ Set cartItems = new HashSet<>();
+ cartItems = cart.getItems();
+ Assert.assertNull(cartItems);
+ Items item1 = new Items();
+ item1.setItemId("I10");
+ item1.setItemTotal(10);
+ item1.setQuantity(1);
+ item1.setCart(cart);
+ assertNotNull(item1);
+ Set itemsSet = new HashSet();
+ itemsSet.add(item1);
+ assertNotNull(itemsSet);
+ cart.setItems(itemsSet);
+ assertNotNull(cart);
+ session.persist(cart);
+ session.getTransaction().commit();
+ session.close();
+
+ }
+
+
}
diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/CartTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/CartTest.java
deleted file mode 100644
index 5d6dbbb5e0..0000000000
--- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/CartTest.java
+++ /dev/null
@@ -1,111 +0,0 @@
-
-package com.baeldung.hibernate.oneToMany.model;
-
-import java.util.Set;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-
-public class CartTest {
-
- public CartTest() {
- }
-
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
- @Test
- public void testGetId() {
- System.out.println("getId");
- Cart instance = new Cart();
- long expResult = 0L;
- long result = instance.getId();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetId() {
- System.out.println("setId");
- long id = 0L;
- Cart instance = new Cart();
- instance.setId(id);
- fail("The test failed.");
- }
-
- @Test
- public void testGetTotal() {
- System.out.println("getTotal");
- Cart instance = new Cart();
- double expResult = 0.0;
- double result = instance.getTotal();
- assertEquals(expResult, result, 0.0);
- fail("The test failed.");
- }
-
- @Test
- public void testSetTotal() {
- System.out.println("setTotal");
- double total = 0.0;
- Cart instance = new Cart();
- instance.setTotal(total);
- fail("The test failed.");
- }
-
- @Test
- public void testGetName() {
- System.out.println("getName");
- Cart instance = new Cart();
- String expResult = "";
- String result = instance.getName();
- assertEquals(expResult, result);
- fail("The test failed");
- }
-
- @Test
- public void testSetName() {
- System.out.println("setName");
- String name = "";
- Cart instance = new Cart();
- instance.setName(name);
- fail("The test failed.");
- }
-
- @Test
- public void testGetItems() {
- System.out.println("getItems");
- Cart instance = new Cart();
- Set expResult = null;
- Set result = instance.getItems();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetItems() {
- System.out.println("setItems");
- Set items = null;
- Cart instance = new Cart();
- instance.setItems(items);
- fail("The test case failed");
- }
-
-
-}
diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/ItemsTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/ItemsTest.java
deleted file mode 100644
index c6e792f9bb..0000000000
--- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/ItemsTest.java
+++ /dev/null
@@ -1,129 +0,0 @@
-
-package com.baeldung.hibernate.oneToMany.model;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class ItemsTest {
-
- public ItemsTest() {
- }
-
- @BeforeClass
- public static void setUpClass() {
- }
-
- @AfterClass
- public static void tearDownClass() {
- }
-
- @Before
- public void setUp() {
- }
-
- @After
- public void tearDown() {
- }
-
- @Test
- public void testGetItemId() {
- System.out.println("getItemId");
- Items instance = new Items();
- String expResult = "";
- String result = instance.getItemId();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetItemId() {
- System.out.println("setItemId");
- String itemId = "";
- Items instance = new Items();
- instance.setItemId(itemId);
- fail("The test failed.");
- }
-
- @Test
- public void testGetItemTotal() {
- System.out.println("getItemTotal");
- Items instance = new Items();
- double expResult = 0.0;
- double result = instance.getItemTotal();
- assertEquals(expResult, result, 0.0);
- fail("The test failed.");
- }
-
- @Test
- public void testSetItemTotal() {
- System.out.println("setItemTotal");
- double itemTotal = 0.0;
- Items instance = new Items();
- instance.setItemTotal(itemTotal);
- fail("The test failed.");
- }
-
- @Test
- public void testGetQuantity() {
- System.out.println("getQuantity");
- Items instance = new Items();
- int expResult = 0;
- int result = instance.getQuantity();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetQuantity() {
- System.out.println("setQuantity");
- int quantity = 0;
- Items instance = new Items();
- instance.setQuantity(quantity);
- fail("The test failed.");
- }
-
- @Test
- public void testGetCart() {
- System.out.println("getCart");
- Items instance = new Items();
- Cart expResult = null;
- Cart result = instance.getCart();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetCart() {
- System.out.println("setCart");
- Cart cart = null;
- Items instance = new Items();
- instance.setCart(cart);
- fail("The test failed.");
- }
-
- @Test
- public void testGetId() {
- System.out.println("getId");
- Items instance = new Items();
- long expResult = 0L;
- long result = instance.getId();
- assertEquals(expResult, result);
- fail("The test failed.");
- }
-
- @Test
- public void testSetId() {
- System.out.println("setId");
- long id = 0L;
- Items instance = new Items();
- instance.setId(id);
- fail("The test failed.");
- }
-
-}