Skip to content

Commit c350e8b

Browse files
mobibluntmaibin
authored andcommitted
Fix to Unit test for Hibernate one to many (eugenp#1085)
* Fix to Unit tests for Hibernate one to many * Fixed Unit test for hibernate one to many mapping tutorial * Fixed Unit test for Hibernate one to many mapping tutorial * Fixed Unit test for Hibernate one to many mapping Tutorial * Fixed Unit Test for Hibernate one to many mapping Tutorial * Unit test for Hibernate One to many Tutorial
1 parent 3522c05 commit c350e8b

6 files changed

Lines changed: 69 additions & 303 deletions

File tree

‎spring-hibernate4/src/main/resources/hibernate-annotation.cfg.xml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
<hibernate-configuration>
66
<session-factory>
77
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
8-
<property name="hibernate.connection.password">bastard5</property>
9-
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/setup</property>
10-
<property name="hibernate.connection.username">root</property>
8+
<property name="hibernate.connection.password">mypassword</property>
9+
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_one_to_many?createDatabaseIfNotExist=true</property>
10+
<property name="hibernate.connection.username">myuser</property>
1111
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
1212

1313
<property name="hibernate.current_session_context_class">thread</property>
1414
<property name="hibernate.show_sql">true</property>
1515

16-
<mapping class="com.baeldung.hibernate.model.Cart"/>
17-
<mapping class="com.baeldung.hibernate.model.Items"/>
16+
<mapping class="com.baeldung.hibernate.oneToMany.model.Cart"/>
17+
<mapping class="com.baeldung.hibernate.oneToMany.model.Items"/>
1818
</session-factory>
1919
</hibernate-configuration>

‎spring-hibernate4/src/main/resources/one_to_many.sql‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ CREATE TABLE `Items` (
1414
PRIMARY KEY (`id`),
1515
KEY `cart_id` (`cart_id`),
1616
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)
17-
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
17+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

‎spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtilTest.java‎

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11

22
package com.baeldung.hibernate.oneToMany.main;
33

4-
import org.junit.After;
5-
import org.junit.AfterClass;
4+
5+
import com.baeldung.hibernate.oneToMany.model.Cart;
6+
import com.baeldung.hibernate.oneToMany.model.Items;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
import static org.hamcrest.Matchers.hasSize;
10+
import org.hibernate.Session;
11+
import org.hibernate.SessionFactory;
12+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
13+
import org.hibernate.cfg.Configuration;
14+
import org.hibernate.dialect.HSQLDialect;
15+
import org.hibernate.service.ServiceRegistry;
16+
import org.junit.Assert;
617
import org.junit.Before;
718
import org.junit.BeforeClass;
819
import org.junit.Test;
@@ -11,30 +22,68 @@
1122

1223
public class HibernateOneToManyAnnotationMainTest {
1324

25+
private static SessionFactory sessionFactory;
26+
27+
private Session session;
28+
1429
public HibernateOneToManyAnnotationMainTest() {
1530
}
1631

17-
@BeforeClass
18-
public static void setUpClass() {
19-
}
2032

21-
@AfterClass
22-
public static void tearDownClass() {
33+
@BeforeClass
34+
public static void beforeTests() {
35+
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())
36+
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
37+
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
38+
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
2339
}
2440

2541
@Before
2642
public void setUp() {
43+
session = sessionFactory.openSession();
44+
session.beginTransaction();
45+
2746
}
2847

29-
@After
30-
public void tearDown() {
48+
49+
50+
@Test
51+
public void testAddItemsToCart() {
52+
Cart cart = new Cart();
53+
Set <Items> cartItems = new HashSet<>();
54+
cartItems = cart.getItems();
55+
Assert.assertNull(cartItems);
56+
Items item1 = new Items("I10", 10, 1, cart);
57+
assertNotNull(item1);
58+
Set<Items> itemsSet = new HashSet<Items>();
59+
cart.setItems(itemsSet);
60+
assertNotNull(cart);
61+
System.out.println("Items added to cart");
62+
3163
}
64+
3265
@Test
33-
public void testMain() {
34-
System.out.println("main");
35-
String[] args = null;
36-
HibernateOneToManyAnnotationMain.main(args);
37-
fail("The test failed.");
66+
public void testSaveCart(){
67+
Cart cart = new Cart();
68+
Set <Items> cartItems = new HashSet<>();
69+
cartItems = cart.getItems();
70+
Assert.assertNull(cartItems);
71+
Items item1 = new Items();
72+
item1.setItemId("I10");
73+
item1.setItemTotal(10);
74+
item1.setQuantity(1);
75+
item1.setCart(cart);
76+
assertNotNull(item1);
77+
Set<Items> itemsSet = new HashSet<Items>();
78+
itemsSet.add(item1);
79+
assertNotNull(itemsSet);
80+
cart.setItems(itemsSet);
81+
assertNotNull(cart);
82+
session.persist(cart);
83+
session.getTransaction().commit();
84+
session.close();
85+
3886
}
3987

88+
4089
}

‎spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/model/CartTest.java‎

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)