Skip to content

Commit d472a7d

Browse files
mobibluntmaibin
authored andcommitted
Hibernate one to many Tutorial (eugenp#1050)
* First Commit * Second Commit for Guide to JGit * Hibernate many to one example * added test units to hibernate one to many tutorial
1 parent a722a46 commit d472a7d

12 files changed

Lines changed: 592 additions & 2 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ target/
2727

2828
spring-openid/src/main/resources/application.properties
2929
.recommenders/
30+
/spring-hibernate4/nbproject/

‎spring-hibernate4/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<dependency>
106106
<groupId>junit</groupId>
107107
<artifactId>junit</artifactId>
108-
<version>${junit.version}</version>
108+
<version>4.12</version>
109109
<scope>test</scope>
110110
</dependency>
111111

@@ -119,7 +119,7 @@
119119
<dependency>
120120
<groupId>org.hamcrest</groupId>
121121
<artifactId>hamcrest-core</artifactId>
122-
<version>${org.hamcrest.version}</version>
122+
<version>1.3</version>
123123
<scope>test</scope>
124124
</dependency>
125125

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hibernate.oneToMany.config;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
5+
import org.hibernate.cfg.Configuration;
6+
import org.hibernate.service.ServiceRegistry;
7+
8+
public class HibernateAnnotationUtil {
9+
10+
private static SessionFactory sessionFactory;
11+
12+
private static SessionFactory buildSessionFactory() {
13+
try {
14+
// Create the SessionFactory from hibernate-annotation.cfg.xml
15+
Configuration configuration = new Configuration();
16+
configuration.configure("hibernate-annotation.cfg.xml");
17+
System.out.println("Hibernate Annotation Configuration loaded");
18+
19+
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
20+
System.out.println("Hibernate Annotation serviceRegistry created");
21+
22+
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
23+
24+
25+
return sessionFactory;
26+
}
27+
catch (Throwable ex) {
28+
System.err.println("Initial SessionFactory creation failed." + ex);
29+
ex.printStackTrace();
30+
throw new ExceptionInInitializerError(ex);
31+
}
32+
}
33+
34+
public static SessionFactory getSessionFactory() {
35+
if(sessionFactory == null) sessionFactory = buildSessionFactory();
36+
return sessionFactory;
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.hibernate.oneToMany.main;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.hibernate.Session;
7+
import org.hibernate.SessionFactory;
8+
import org.hibernate.Transaction;
9+
10+
import com.baeldung.hibernate.model.Cart;
11+
import com.baeldung.hibernate.model.Items;
12+
import com.baeldung.hibernate.config.HibernateAnnotationUtil;
13+
14+
public class HibernateOneToManyAnnotationMain {
15+
16+
public static void main(String[] args) {
17+
18+
Cart cart = new Cart();
19+
cart.setName("MyCart");
20+
21+
Items item1 = new Items("I10", 10, 1, cart);
22+
Items item2 = new Items("I20", 20, 2, cart);
23+
Set<Items> itemsSet = new HashSet<Items>();
24+
itemsSet.add(item1); itemsSet.add(item2);
25+
26+
cart.setItems(itemsSet);
27+
cart.setTotal(10*1 + 20*2);
28+
29+
SessionFactory sessionFactory = null;
30+
Session session = null;
31+
Transaction tx = null;
32+
try{
33+
//Get Session
34+
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
35+
session = sessionFactory.getCurrentSession();
36+
System.out.println("Session created");
37+
//start transaction
38+
tx = session.beginTransaction();
39+
//Save the Model object
40+
session.save(cart);
41+
session.save(item1);
42+
session.save(item2);
43+
//Commit transaction
44+
tx.commit();
45+
System.out.println("Cart ID="+cart.getId());
46+
System.out.println("item1 ID="+item1.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
47+
System.out.println("item2 ID="+item2.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
48+
49+
}catch(Exception e){
50+
System.out.println("Exception occured. "+e.getMessage());
51+
e.printStackTrace();
52+
}finally{
53+
if(!sessionFactory.isClosed()){
54+
System.out.println("Closing SessionFactory");
55+
sessionFactory.close();
56+
}
57+
}
58+
}
59+
60+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.hibernate.oneToMany.model;
2+
3+
import java.util.Set;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToMany;
11+
import javax.persistence.Table;
12+
13+
@Entity
14+
@Table(name="CART")
15+
public class Cart {
16+
17+
@Id
18+
@GeneratedValue(strategy=GenerationType.IDENTITY)
19+
@Column(name="cart_id")
20+
private long id;
21+
22+
@Column(name="total")
23+
private double total;
24+
25+
@Column(name="name")
26+
private String name;
27+
28+
@OneToMany(mappedBy="cart")
29+
private Set<Items> items;
30+
31+
public long getId() {
32+
return id;
33+
}
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
public double getTotal() {
38+
return total;
39+
}
40+
public void setTotal(double total) {
41+
this.total = total;
42+
}
43+
public String getName() {
44+
return name;
45+
}
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
public Set<Items> getItems() {
50+
return items;
51+
}
52+
public void setItems(Set<Items> items) {
53+
this.items = items;
54+
}
55+
56+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.hibernate.oneToMany.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name="ITEMS")
14+
public class Items {
15+
16+
@Id
17+
@GeneratedValue(strategy=GenerationType.IDENTITY)
18+
@Column(name="id")
19+
private long id;
20+
21+
@Column(name="item_id")
22+
private String itemId;
23+
24+
@Column(name="item_total")
25+
private double itemTotal;
26+
27+
@Column(name="quantity")
28+
private int quantity;
29+
30+
@ManyToOne
31+
@JoinColumn(name="cart_id", nullable=false)
32+
private Cart cart;
33+
34+
//Hibernate requires no-args constructor
35+
public Items(){}
36+
37+
public Items(String itemId, double total, int qty, Cart c){
38+
this.itemId=itemId;
39+
this.itemTotal=total;
40+
this.quantity=qty;
41+
this.cart=c;
42+
}
43+
public String getItemId() {
44+
return itemId;
45+
}
46+
public void setItemId(String itemId) {
47+
this.itemId = itemId;
48+
}
49+
public double getItemTotal() {
50+
return itemTotal;
51+
}
52+
public void setItemTotal(double itemTotal) {
53+
this.itemTotal = itemTotal;
54+
}
55+
public int getQuantity() {
56+
return quantity;
57+
}
58+
public void setQuantity(int quantity) {
59+
this.quantity = quantity;
60+
}
61+
public Cart getCart() {
62+
return cart;
63+
}
64+
public void setCart(Cart cart) {
65+
this.cart = cart;
66+
}
67+
public long getId() {
68+
return id;
69+
}
70+
public void setId(long id) {
71+
this.id = id;
72+
}
73+
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5+
<hibernate-configuration>
6+
<session-factory>
7+
<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>
11+
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
12+
13+
<property name="hibernate.current_session_context_class">thread</property>
14+
<property name="hibernate.show_sql">true</property>
15+
16+
<mapping class="com.baeldung.hibernate.model.Cart"/>
17+
<mapping class="com.baeldung.hibernate.model.Items"/>
18+
</session-factory>
19+
</hibernate-configuration>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE `Cart` (
2+
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
3+
`total` decimal(10,0) NOT NULL,
4+
`name` varchar(10) DEFAULT NULL,
5+
PRIMARY KEY (`cart_id`)
6+
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
7+
8+
CREATE TABLE `Items` (
9+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
10+
`cart_id` int(11) unsigned NOT NULL,
11+
`item_id` varchar(10) NOT NULL,
12+
`item_total` decimal(10,0) NOT NULL,
13+
`quantity` int(3) NOT NULL,
14+
PRIMARY KEY (`id`),
15+
KEY `cart_id` (`cart_id`),
16+
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)
17+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
package com.baeldung.hibernate.oneToMany.config;
3+
4+
import org.hibernate.SessionFactory;
5+
import org.junit.After;
6+
import org.junit.AfterClass;
7+
import org.junit.Before;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
import static org.junit.Assert.*;
11+
12+
13+
public class HibernateAnnotationUtilTest {
14+
15+
public HibernateAnnotationUtilTest() {
16+
}
17+
18+
@BeforeClass
19+
public static void setUpClass() {
20+
}
21+
22+
@AfterClass
23+
public static void tearDownClass() {
24+
}
25+
26+
@Before
27+
public void setUp() {
28+
}
29+
30+
@After
31+
public void tearDown() {
32+
}
33+
34+
@Test
35+
public void testGetSessionFactory() {
36+
System.out.println("getSessionFactory");
37+
SessionFactory expResult = null;
38+
SessionFactory result = HibernateAnnotationUtil.getSessionFactory();
39+
assertEquals(expResult, result);
40+
fail("The test failed.");
41+
}
42+
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
package com.baeldung.hibernate.oneToMany.main;
3+
4+
import org.junit.After;
5+
import org.junit.AfterClass;
6+
import org.junit.Before;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
import static org.junit.Assert.*;
10+
11+
12+
public class HibernateOneToManyAnnotationMainTest {
13+
14+
public HibernateOneToManyAnnotationMainTest() {
15+
}
16+
17+
@BeforeClass
18+
public static void setUpClass() {
19+
}
20+
21+
@AfterClass
22+
public static void tearDownClass() {
23+
}
24+
25+
@Before
26+
public void setUp() {
27+
}
28+
29+
@After
30+
public void tearDown() {
31+
}
32+
@Test
33+
public void testMain() {
34+
System.out.println("main");
35+
String[] args = null;
36+
HibernateOneToManyAnnotationMain.main(args);
37+
fail("The test failed.");
38+
}
39+
40+
}

0 commit comments

Comments
 (0)