minor cleanup work - formatting
This commit is contained in:
parent
4d5624161f
commit
bd6fe7269e
@ -7,32 +7,31 @@ import org.hibernate.service.ServiceRegistry;
|
|||||||
|
|
||||||
public class HibernateAnnotationUtil {
|
public class HibernateAnnotationUtil {
|
||||||
|
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
private static SessionFactory buildSessionFactory() {
|
||||||
try {
|
try {
|
||||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
configuration.configure("hibernate-annotation.cfg.xml");
|
configuration.configure("hibernate-annotation.cfg.xml");
|
||||||
System.out.println("Hibernate Annotation Configuration loaded");
|
System.out.println("Hibernate Annotation Configuration loaded");
|
||||||
|
|
||||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||||
System.out.println("Hibernate Annotation serviceRegistry created");
|
System.out.println("Hibernate Annotation serviceRegistry created");
|
||||||
|
|
||||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
|
||||||
|
|
||||||
|
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
|
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
} catch (Throwable ex) {
|
||||||
catch (Throwable ex) {
|
|
||||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
throw new ExceptionInInitializerError(ex);
|
throw new ExceptionInInitializerError(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
public static SessionFactory getSessionFactory() {
|
||||||
if(sessionFactory == null) sessionFactory = buildSessionFactory();
|
if (sessionFactory == null)
|
||||||
|
sessionFactory = buildSessionFactory();
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,48 +13,49 @@ import com.baeldung.hibernate.oneToMany.model.Items;
|
|||||||
|
|
||||||
public class HibernateOneToManyAnnotationMain {
|
public class HibernateOneToManyAnnotationMain {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Cart cart = new Cart();
|
Cart cart = new Cart();
|
||||||
cart.setName("MyCart");
|
cart.setName("MyCart");
|
||||||
|
|
||||||
Items item1 = new Items("I10", 10, 1, cart);
|
Items item1 = new Items("I10", 10, 1, cart);
|
||||||
Items item2 = new Items("I20", 20, 2, cart);
|
Items item2 = new Items("I20", 20, 2, cart);
|
||||||
Set<Items> itemsSet = new HashSet<Items>();
|
Set<Items> itemsSet = new HashSet<Items>();
|
||||||
itemsSet.add(item1); itemsSet.add(item2);
|
itemsSet.add(item1);
|
||||||
|
itemsSet.add(item2);
|
||||||
|
|
||||||
cart.setItems(itemsSet);
|
cart.setItems(itemsSet);
|
||||||
cart.setTotal(10*1 + 20*2);
|
cart.setTotal(10 * 1 + 20 * 2);
|
||||||
|
|
||||||
SessionFactory sessionFactory = null;
|
SessionFactory sessionFactory = null;
|
||||||
Session session = null;
|
Session session = null;
|
||||||
Transaction tx = null;
|
Transaction tx = null;
|
||||||
try{
|
try {
|
||||||
//Get Session
|
// Get Session
|
||||||
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
||||||
session = sessionFactory.getCurrentSession();
|
session = sessionFactory.getCurrentSession();
|
||||||
System.out.println("Session created");
|
System.out.println("Session created");
|
||||||
//start transaction
|
// start transaction
|
||||||
tx = session.beginTransaction();
|
tx = session.beginTransaction();
|
||||||
//Save the Model object
|
// Save the Model object
|
||||||
session.save(cart);
|
session.save(cart);
|
||||||
session.save(item1);
|
session.save(item1);
|
||||||
session.save(item2);
|
session.save(item2);
|
||||||
//Commit transaction
|
// Commit transaction
|
||||||
tx.commit();
|
tx.commit();
|
||||||
System.out.println("Cart ID="+cart.getId());
|
System.out.println("Cart ID=" + cart.getId());
|
||||||
System.out.println("item1 ID="+item1.getId()+", Foreign Key Cart ID="+item1.getCart().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());
|
System.out.println("item2 ID=" + item2.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
|
||||||
|
|
||||||
}catch(Exception e){
|
} catch (Exception e) {
|
||||||
System.out.println("Exception occured. "+e.getMessage());
|
System.out.println("Exception occured. " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}finally{
|
} finally {
|
||||||
if(!sessionFactory.isClosed()){
|
if (!sessionFactory.isClosed()) {
|
||||||
System.out.println("Closing SessionFactory");
|
System.out.println("Closing SessionFactory");
|
||||||
sessionFactory.close();
|
sessionFactory.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,46 +11,53 @@ import javax.persistence.OneToMany;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="CART")
|
@Table(name = "CART")
|
||||||
public class Cart {
|
public class Cart {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name="cart_id")
|
@Column(name = "cart_id")
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name="total")
|
@Column(name = "total")
|
||||||
private double total;
|
private double total;
|
||||||
|
|
||||||
@Column(name="name")
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy="cart")
|
@OneToMany(mappedBy = "cart")
|
||||||
private Set<Items> items;
|
private Set<Items> items;
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
public void setId(long id) {
|
|
||||||
this.id = id;
|
public void setId(long id) {
|
||||||
}
|
this.id = id;
|
||||||
public double getTotal() {
|
}
|
||||||
return total;
|
|
||||||
}
|
public double getTotal() {
|
||||||
public void setTotal(double total) {
|
return total;
|
||||||
this.total = total;
|
}
|
||||||
}
|
|
||||||
public String getName() {
|
public void setTotal(double total) {
|
||||||
return name;
|
this.total = total;
|
||||||
}
|
}
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
public String getName() {
|
||||||
}
|
return name;
|
||||||
public Set<Items> getItems() {
|
}
|
||||||
return items;
|
|
||||||
}
|
public void setName(String name) {
|
||||||
public void setItems(Set<Items> items) {
|
this.name = name;
|
||||||
this.items = items;
|
}
|
||||||
}
|
|
||||||
|
public Set<Items> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(Set<Items> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,65 +10,76 @@ import javax.persistence.ManyToOne;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="ITEMS")
|
@Table(name = "ITEMS")
|
||||||
public class Items {
|
public class Items {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name="id")
|
@Column(name = "id")
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name="item_id")
|
@Column(name = "item_id")
|
||||||
private String itemId;
|
private String itemId;
|
||||||
|
|
||||||
@Column(name="item_total")
|
@Column(name = "item_total")
|
||||||
private double itemTotal;
|
private double itemTotal;
|
||||||
|
|
||||||
@Column(name="quantity")
|
@Column(name = "quantity")
|
||||||
private int quantity;
|
private int quantity;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="cart_id", nullable=false)
|
@JoinColumn(name = "cart_id", nullable = false)
|
||||||
private Cart cart;
|
private Cart cart;
|
||||||
|
|
||||||
//Hibernate requires no-args constructor
|
// Hibernate requires no-args constructor
|
||||||
public Items(){}
|
public Items() {
|
||||||
|
}
|
||||||
|
|
||||||
public Items(String itemId, double total, int qty, Cart c){
|
public Items(String itemId, double total, int qty, Cart c) {
|
||||||
this.itemId=itemId;
|
this.itemId = itemId;
|
||||||
this.itemTotal=total;
|
this.itemTotal = total;
|
||||||
this.quantity=qty;
|
this.quantity = qty;
|
||||||
this.cart=c;
|
this.cart = c;
|
||||||
}
|
}
|
||||||
public String getItemId() {
|
|
||||||
return itemId;
|
public String getItemId() {
|
||||||
}
|
return itemId;
|
||||||
public void setItemId(String itemId) {
|
}
|
||||||
this.itemId = itemId;
|
|
||||||
}
|
public void setItemId(String itemId) {
|
||||||
public double getItemTotal() {
|
this.itemId = itemId;
|
||||||
return itemTotal;
|
}
|
||||||
}
|
|
||||||
public void setItemTotal(double itemTotal) {
|
public double getItemTotal() {
|
||||||
this.itemTotal = itemTotal;
|
return itemTotal;
|
||||||
}
|
}
|
||||||
public int getQuantity() {
|
|
||||||
return quantity;
|
public void setItemTotal(double itemTotal) {
|
||||||
}
|
this.itemTotal = itemTotal;
|
||||||
public void setQuantity(int quantity) {
|
}
|
||||||
this.quantity = quantity;
|
|
||||||
}
|
public int getQuantity() {
|
||||||
public Cart getCart() {
|
return quantity;
|
||||||
return cart;
|
}
|
||||||
}
|
|
||||||
public void setCart(Cart cart) {
|
public void setQuantity(int quantity) {
|
||||||
this.cart = cart;
|
this.quantity = quantity;
|
||||||
}
|
}
|
||||||
public long getId() {
|
|
||||||
return id;
|
public Cart getCart() {
|
||||||
}
|
return cart;
|
||||||
public void setId(long id) {
|
}
|
||||||
this.id = id;
|
|
||||||
}
|
public void setCart(Cart cart) {
|
||||||
|
this.cart = cart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
|
|
||||||
package com.baeldung.hibernate.oneToMany.main;
|
package com.baeldung.hibernate.oneToMany.main;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
import com.baeldung.hibernate.oneToMany.model.Cart;
|
|
||||||
import com.baeldung.hibernate.oneToMany.model.Items;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
@ -17,9 +17,9 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import org.junit.runner.RunWith;
|
import com.baeldung.hibernate.oneToMany.model.Cart;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import com.baeldung.hibernate.oneToMany.model.Items;
|
||||||
|
|
||||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
public class HibernateOneToManyAnnotationMainTest {
|
public class HibernateOneToManyAnnotationMainTest {
|
||||||
@ -31,11 +31,11 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||||||
public HibernateOneToManyAnnotationMainTest() {
|
public HibernateOneToManyAnnotationMainTest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeTests() {
|
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())
|
Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class).setProperty("hibernate.dialect", HSQLDialect.class.getName())
|
||||||
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
|
.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();
|
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||||
}
|
}
|
||||||
@ -48,20 +48,16 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSession_checkIfDatabaseIsEmpty(){
|
public void givenSession_checkIfDatabaseIsEmpty() {
|
||||||
Cart cart = (Cart) session.get(Cart.class, new Long(1));
|
Cart cart = (Cart) session.get(Cart.class, new Long(1));
|
||||||
assertNull(cart);
|
assertNull(cart);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddItemsToCart() {
|
public void testAddItemsToCart() {
|
||||||
Cart cart = new Cart();
|
Cart cart = new Cart();
|
||||||
Set <Items> cartItems = new HashSet<>();
|
Set<Items> cartItems = new HashSet<>();
|
||||||
cartItems = cart.getItems();
|
cartItems = cart.getItems();
|
||||||
Assert.assertNull(cartItems);
|
Assert.assertNull(cartItems);
|
||||||
Items item1 = new Items("I10", 10, 1, cart);
|
Items item1 = new Items("I10", 10, 1, cart);
|
||||||
@ -74,29 +70,28 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSession_checkIfDatabaseIsPopulated_afterCommit(){
|
public void givenSession_checkIfDatabaseIsPopulated_afterCommit() {
|
||||||
Cart cart = new Cart();
|
Cart cart = new Cart();
|
||||||
Set <Items> cartItems = new HashSet<>();
|
Set<Items> cartItems = new HashSet<>();
|
||||||
cartItems = cart.getItems();
|
cartItems = cart.getItems();
|
||||||
Assert.assertNull(cartItems);
|
Assert.assertNull(cartItems);
|
||||||
Items item1 = new Items();
|
Items item1 = new Items();
|
||||||
item1.setItemId("I10");
|
item1.setItemId("I10");
|
||||||
item1.setItemTotal(10);
|
item1.setItemTotal(10);
|
||||||
item1.setQuantity(1);
|
item1.setQuantity(1);
|
||||||
item1.setCart(cart);
|
item1.setCart(cart);
|
||||||
assertNotNull(item1);
|
assertNotNull(item1);
|
||||||
Set<Items> itemsSet = new HashSet<Items>();
|
Set<Items> itemsSet = new HashSet<Items>();
|
||||||
itemsSet.add(item1);
|
itemsSet.add(item1);
|
||||||
assertNotNull(itemsSet);
|
assertNotNull(itemsSet);
|
||||||
cart.setItems(itemsSet);
|
cart.setItems(itemsSet);
|
||||||
assertNotNull(cart);
|
assertNotNull(cart);
|
||||||
session.persist(cart);
|
session.persist(cart);
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
cart = (Cart) session.get(Cart.class, new Long(1));
|
cart = (Cart) session.get(Cart.class, new Long(1));
|
||||||
assertNotNull(cart);
|
assertNotNull(cart);
|
||||||
session.close();
|
session.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user