Hibernate one to many Tutorial (#1050)
* First Commit * Second Commit for Guide to JGit * Hibernate many to one example * added test units to hibernate one to many tutorial
This commit is contained in:
parent
a722a46373
commit
d472a7dfa0
1
.gitignore
vendored
1
.gitignore
vendored
@ -27,3 +27,4 @@ target/
|
||||
|
||||
spring-openid/src/main/resources/application.properties
|
||||
.recommenders/
|
||||
/spring-hibernate4/nbproject/
|
@ -105,7 +105,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@ -119,7 +119,7 @@
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<version>${org.hamcrest.version}</version>
|
||||
<version>1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.hibernate.oneToMany.config;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
public class HibernateAnnotationUtil {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private static SessionFactory buildSessionFactory() {
|
||||
try {
|
||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.configure("hibernate-annotation.cfg.xml");
|
||||
System.out.println("Hibernate Annotation Configuration loaded");
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||
System.out.println("Hibernate Annotation serviceRegistry created");
|
||||
|
||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||
ex.printStackTrace();
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
if(sessionFactory == null) sessionFactory = buildSessionFactory();
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.baeldung.hibernate.oneToMany.main;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import com.baeldung.hibernate.model.Cart;
|
||||
import com.baeldung.hibernate.model.Items;
|
||||
import com.baeldung.hibernate.config.HibernateAnnotationUtil;
|
||||
|
||||
public class HibernateOneToManyAnnotationMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Cart cart = new Cart();
|
||||
cart.setName("MyCart");
|
||||
|
||||
Items item1 = new Items("I10", 10, 1, cart);
|
||||
Items item2 = new Items("I20", 20, 2, cart);
|
||||
Set<Items> itemsSet = new HashSet<Items>();
|
||||
itemsSet.add(item1); itemsSet.add(item2);
|
||||
|
||||
cart.setItems(itemsSet);
|
||||
cart.setTotal(10*1 + 20*2);
|
||||
|
||||
SessionFactory sessionFactory = null;
|
||||
Session session = null;
|
||||
Transaction tx = null;
|
||||
try{
|
||||
//Get Session
|
||||
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
||||
session = sessionFactory.getCurrentSession();
|
||||
System.out.println("Session created");
|
||||
//start transaction
|
||||
tx = session.beginTransaction();
|
||||
//Save the Model object
|
||||
session.save(cart);
|
||||
session.save(item1);
|
||||
session.save(item2);
|
||||
//Commit transaction
|
||||
tx.commit();
|
||||
System.out.println("Cart ID="+cart.getId());
|
||||
System.out.println("item1 ID="+item1.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
|
||||
System.out.println("item2 ID="+item2.getId()+", Foreign Key Cart ID="+item1.getCart().getId());
|
||||
|
||||
}catch(Exception e){
|
||||
System.out.println("Exception occured. "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
if(!sessionFactory.isClosed()){
|
||||
System.out.println("Closing SessionFactory");
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="CART")
|
||||
public class Cart {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="cart_id")
|
||||
private long id;
|
||||
|
||||
@Column(name="total")
|
||||
private double total;
|
||||
|
||||
@Column(name="name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy="cart")
|
||||
private Set<Items> items;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public double getTotal() {
|
||||
return total;
|
||||
}
|
||||
public void setTotal(double total) {
|
||||
this.total = total;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Set<Items> getItems() {
|
||||
return items;
|
||||
}
|
||||
public void setItems(Set<Items> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="ITEMS")
|
||||
public class Items {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
private long id;
|
||||
|
||||
@Column(name="item_id")
|
||||
private String itemId;
|
||||
|
||||
@Column(name="item_total")
|
||||
private double itemTotal;
|
||||
|
||||
@Column(name="quantity")
|
||||
private int quantity;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="cart_id", nullable=false)
|
||||
private Cart cart;
|
||||
|
||||
//Hibernate requires no-args constructor
|
||||
public Items(){}
|
||||
|
||||
public Items(String itemId, double total, int qty, Cart c){
|
||||
this.itemId=itemId;
|
||||
this.itemTotal=total;
|
||||
this.quantity=qty;
|
||||
this.cart=c;
|
||||
}
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
public void setItemId(String itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
public double getItemTotal() {
|
||||
return itemTotal;
|
||||
}
|
||||
public void setItemTotal(double itemTotal) {
|
||||
this.itemTotal = itemTotal;
|
||||
}
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
public Cart getCart() {
|
||||
return cart;
|
||||
}
|
||||
public void setCart(Cart cart) {
|
||||
this.cart = cart;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
|
||||
<property name="hibernate.connection.password">bastard5</property>
|
||||
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/setup</property>
|
||||
<property name="hibernate.connection.username">root</property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
|
||||
<property name="hibernate.current_session_context_class">thread</property>
|
||||
<property name="hibernate.show_sql">true</property>
|
||||
|
||||
<mapping class="com.baeldung.hibernate.model.Cart"/>
|
||||
<mapping class="com.baeldung.hibernate.model.Items"/>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
17
spring-hibernate4/src/main/resources/one_to_many.sql
Normal file
17
spring-hibernate4/src/main/resources/one_to_many.sql
Normal file
@ -0,0 +1,17 @@
|
||||
CREATE TABLE `Cart` (
|
||||
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`total` decimal(10,0) NOT NULL,
|
||||
`name` varchar(10) DEFAULT NULL,
|
||||
PRIMARY KEY (`cart_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `Items` (
|
||||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`cart_id` int(11) unsigned NOT NULL,
|
||||
`item_id` varchar(10) NOT NULL,
|
||||
`item_total` decimal(10,0) NOT NULL,
|
||||
`quantity` int(3) NOT NULL,
|
||||
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;
|
@ -0,0 +1,43 @@
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
package com.baeldung.hibernate.oneToMany.main;
|
||||
|
||||
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 HibernateOneToManyAnnotationMainTest {
|
||||
|
||||
public HibernateOneToManyAnnotationMainTest() {
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
@Test
|
||||
public void testMain() {
|
||||
System.out.println("main");
|
||||
String[] args = null;
|
||||
HibernateOneToManyAnnotationMain.main(args);
|
||||
fail("The test failed.");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
|
||||
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<Items> expResult = null;
|
||||
Set<Items> result = instance.getItems();
|
||||
assertEquals(expResult, result);
|
||||
fail("The test failed.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetItems() {
|
||||
System.out.println("setItems");
|
||||
Set<Items> items = null;
|
||||
Cart instance = new Cart();
|
||||
instance.setItems(items);
|
||||
fail("The test case failed");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
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 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.setCart1(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.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user