minor cleanup work - formatting

This commit is contained in:
eugenp 2017-02-04 14:14:23 +02:00
parent 4d5624161f
commit bd6fe7269e
5 changed files with 221 additions and 208 deletions

View File

@ -21,10 +21,8 @@ public class HibernateAnnotationUtil {
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);
@ -32,7 +30,8 @@ public class HibernateAnnotationUtil {
} }
public static SessionFactory getSessionFactory() { public static SessionFactory getSessionFactory() {
if(sessionFactory == null) sessionFactory = buildSessionFactory(); if (sessionFactory == null)
sessionFactory = buildSessionFactory();
return sessionFactory; return sessionFactory;
} }
} }

View File

@ -21,7 +21,8 @@ public class HibernateOneToManyAnnotationMain {
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);

View File

@ -31,24 +31,31 @@ public class Cart {
public long getId() { public long getId() {
return id; return id;
} }
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public double getTotal() { public double getTotal() {
return total; return total;
} }
public void setTotal(double total) { public void setTotal(double total) {
this.total = total; this.total = total;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Set<Items> getItems() { public Set<Items> getItems() {
return items; return items;
} }
public void setItems(Set<Items> items) { public void setItems(Set<Items> items) {
this.items = items; this.items = items;
} }

View File

@ -32,7 +32,8 @@ public class Items {
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;
@ -40,33 +41,43 @@ public class Items {
this.quantity = qty; this.quantity = qty;
this.cart = c; this.cart = c;
} }
public String getItemId() { public String getItemId() {
return itemId; return itemId;
} }
public void setItemId(String itemId) { public void setItemId(String itemId) {
this.itemId = itemId; this.itemId = itemId;
} }
public double getItemTotal() { public double getItemTotal() {
return itemTotal; return itemTotal;
} }
public void setItemTotal(double itemTotal) { public void setItemTotal(double itemTotal) {
this.itemTotal = itemTotal; this.itemTotal = itemTotal;
} }
public int getQuantity() { public int getQuantity() {
return quantity; return quantity;
} }
public void setQuantity(int quantity) { public void setQuantity(int quantity) {
this.quantity = quantity; this.quantity = quantity;
} }
public Cart getCart() { public Cart getCart() {
return cart; return cart;
} }
public void setCart(Cart cart) { public void setCart(Cart cart) {
this.cart = cart; this.cart = cart;
} }
public long getId() { public long getId() {
return id; return id;
} }
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }

View File

@ -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);
} }
@ -54,10 +54,6 @@ public class HibernateOneToManyAnnotationMainTest {
} }
@Test @Test
public void testAddItemsToCart() { public void testAddItemsToCart() {
Cart cart = new Cart(); Cart cart = new Cart();
@ -98,5 +94,4 @@ public class HibernateOneToManyAnnotationMainTest {
} }
} }