minor cleanup work - formatting
This commit is contained in:
parent
4d5624161f
commit
bd6fe7269e
|
@ -21,10 +21,8 @@ public class HibernateAnnotationUtil {
|
|||
|
||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
} catch (Throwable ex) {
|
||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
||||
ex.printStackTrace();
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
|
@ -32,7 +30,8 @@ public class HibernateAnnotationUtil {
|
|||
}
|
||||
|
||||
public static SessionFactory getSessionFactory() {
|
||||
if(sessionFactory == null) sessionFactory = buildSessionFactory();
|
||||
if (sessionFactory == null)
|
||||
sessionFactory = buildSessionFactory();
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,36 +21,37 @@ public class HibernateOneToManyAnnotationMain {
|
|||
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);
|
||||
itemsSet.add(item1);
|
||||
itemsSet.add(item2);
|
||||
|
||||
cart.setItems(itemsSet);
|
||||
cart.setTotal(10*1 + 20*2);
|
||||
cart.setTotal(10 * 1 + 20 * 2);
|
||||
|
||||
SessionFactory sessionFactory = null;
|
||||
Session session = null;
|
||||
Transaction tx = null;
|
||||
try{
|
||||
//Get Session
|
||||
try {
|
||||
// Get Session
|
||||
sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
||||
session = sessionFactory.getCurrentSession();
|
||||
System.out.println("Session created");
|
||||
//start transaction
|
||||
// start transaction
|
||||
tx = session.beginTransaction();
|
||||
//Save the Model object
|
||||
// Save the Model object
|
||||
session.save(cart);
|
||||
session.save(item1);
|
||||
session.save(item2);
|
||||
//Commit transaction
|
||||
// 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());
|
||||
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());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception occured. " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
if(!sessionFactory.isClosed()){
|
||||
} finally {
|
||||
if (!sessionFactory.isClosed()) {
|
||||
System.out.println("Closing SessionFactory");
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
|
|
@ -11,44 +11,51 @@ import javax.persistence.OneToMany;
|
|||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="CART")
|
||||
@Table(name = "CART")
|
||||
public class Cart {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="cart_id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "cart_id")
|
||||
private long id;
|
||||
|
||||
@Column(name="total")
|
||||
@Column(name = "total")
|
||||
private double total;
|
||||
|
||||
@Column(name="name")
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy="cart")
|
||||
@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;
|
||||
}
|
||||
|
|
|
@ -10,63 +10,74 @@ import javax.persistence.ManyToOne;
|
|||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="ITEMS")
|
||||
@Table(name = "ITEMS")
|
||||
public class Items {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||
@Column(name="id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name="item_id")
|
||||
@Column(name = "item_id")
|
||||
private String itemId;
|
||||
|
||||
@Column(name="item_total")
|
||||
@Column(name = "item_total")
|
||||
private double itemTotal;
|
||||
|
||||
@Column(name="quantity")
|
||||
@Column(name = "quantity")
|
||||
private int quantity;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="cart_id", nullable=false)
|
||||
@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;
|
||||
// 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;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
|
||||
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.Set;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
|
@ -17,9 +17,9 @@ import org.junit.Assert;
|
|||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.hibernate.oneToMany.model.Cart;
|
||||
import com.baeldung.hibernate.oneToMany.model.Items;
|
||||
|
||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class HibernateOneToManyAnnotationMainTest {
|
||||
|
@ -31,11 +31,11 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||
public HibernateOneToManyAnnotationMainTest() {
|
||||
}
|
||||
|
||||
|
||||
@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");
|
||||
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);
|
||||
}
|
||||
|
@ -48,20 +48,16 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenSession_checkIfDatabaseIsEmpty(){
|
||||
public void givenSession_checkIfDatabaseIsEmpty() {
|
||||
Cart cart = (Cart) session.get(Cart.class, new Long(1));
|
||||
assertNull(cart);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testAddItemsToCart() {
|
||||
Cart cart = new Cart();
|
||||
Set <Items> cartItems = new HashSet<>();
|
||||
Set<Items> cartItems = new HashSet<>();
|
||||
cartItems = cart.getItems();
|
||||
Assert.assertNull(cartItems);
|
||||
Items item1 = new Items("I10", 10, 1, cart);
|
||||
|
@ -74,9 +70,9 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenSession_checkIfDatabaseIsPopulated_afterCommit(){
|
||||
public void givenSession_checkIfDatabaseIsPopulated_afterCommit() {
|
||||
Cart cart = new Cart();
|
||||
Set <Items> cartItems = new HashSet<>();
|
||||
Set<Items> cartItems = new HashSet<>();
|
||||
cartItems = cart.getItems();
|
||||
Assert.assertNull(cartItems);
|
||||
Items item1 = new Items();
|
||||
|
@ -98,5 +94,4 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue