diff --git a/persistence-modules/hibernate-annotations/pom.xml b/persistence-modules/hibernate-annotations/pom.xml index ed158216fb..023e5aa30f 100644 --- a/persistence-modules/hibernate-annotations/pom.xml +++ b/persistence-modules/hibernate-annotations/pom.xml @@ -82,7 +82,7 @@ 5.0.2.RELEASE 1.10.6.RELEASE - 5.4.7.Final + 5.6.7.Final true 2.1.7.RELEASE 1.4.200 diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtil.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtil.java index 46e6824f42..6c94d34339 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtil.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/config/HibernateAnnotationUtil.java @@ -1,34 +1,51 @@ package com.baeldung.hibernate.oneToMany.config; +import com.baeldung.hibernate.oneToMany.model.Cart; +import com.baeldung.hibernate.oneToMany.model.CartOIO; +import com.baeldung.hibernate.oneToMany.model.Item; +import com.baeldung.hibernate.oneToMany.model.ItemOIO; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class HibernateAnnotationUtil { + private static final Logger LOGGER = LoggerFactory.getLogger(HibernateAnnotationUtil.class); + private static SessionFactory sessionFactory; + public static SessionFactory getSessionFactory() { + if (sessionFactory == null) { + sessionFactory = buildSessionFactory(); + } + return sessionFactory; + } + private static SessionFactory buildSessionFactory() { try { - // Create the SessionFactory from hibernate-annotation.cfg.xml - ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure("hibernate-annotation.cfg.xml").build(); - Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build(); - SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() + .configure("hibernate-annotation.cfg.xml") + .build(); - return sessionFactory; + Metadata metadata = new MetadataSources(serviceRegistry) + .addAnnotatedClass(Cart.class) + .addAnnotatedClass(CartOIO.class) + .addAnnotatedClass(Item.class) + .addAnnotatedClass(ItemOIO.class) + .getMetadataBuilder() + .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE) + .build(); + + return metadata.getSessionFactoryBuilder().build(); } catch (Throwable ex) { - System.err.println("Initial SessionFactory creation failed." + ex); - ex.printStackTrace(); + LOGGER.error("Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError(ex); } } - - public static SessionFactory getSessionFactory() { - if (sessionFactory == null) - sessionFactory = buildSessionFactory(); - return sessionFactory; - } } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyIsOwningSide.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyIsOwningSide.java new file mode 100644 index 0000000000..f74aecbb92 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyIsOwningSide.java @@ -0,0 +1,71 @@ +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.oneToMany.config.HibernateAnnotationUtil; +import com.baeldung.hibernate.oneToMany.model.Cart; +import com.baeldung.hibernate.oneToMany.model.Item; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HibernateManyIsOwningSide { + + private static final Logger LOGGER = LoggerFactory.getLogger(HibernateManyIsOwningSide.class); + + public static void main(String[] args) { + + Cart cart = new Cart(); + Cart cart2 = new Cart(); + + Item item1 = new Item(cart); + Item item2 = new Item(cart2); + + Set itemsSet = new HashSet<>(); + itemsSet.add(item1); + itemsSet.add(item2); + cart.setItems(itemsSet); + + // Get Session + SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory(); + Session session = sessionFactory.getCurrentSession(); + + try { + LOGGER.info("Session created"); + + // start transaction + Transaction tx = session.beginTransaction(); + + // Save the Model object + session.save(cart); + session.save(cart2); + session.save(item1); + session.save(item2); + + // Commit transaction + tx.commit(); + + session = sessionFactory.getCurrentSession(); + tx = session.beginTransaction(); + + item1 = session.get(Item.class, 1L); + item2 = session.get(Item.class, 2L); + tx.commit(); + + LOGGER.info("item1 ID={}, Foreign Key CartOIO ID={}", item1.getId(), item1.getCart().getId()); + LOGGER.info("item2 ID={}, Foreign Key CartOIO ID={}", item2.getId(), item2.getCart().getId()); + + } catch (Exception e) { + LOGGER.error("Exception occurred", e); + } finally { + if (!sessionFactory.isClosed()) { + LOGGER.info("Closing SessionFactory"); + sessionFactory.close(); + } + } + } +} diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyisOwningSide.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyisOwningSide.java deleted file mode 100644 index 372fb2fc07..0000000000 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateManyisOwningSide.java +++ /dev/null @@ -1,71 +0,0 @@ -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.oneToMany.config.HibernateAnnotationUtil; -import com.baeldung.hibernate.oneToMany.model.Cart; -import com.baeldung.hibernate.oneToMany.model.Items; -import com.baeldung.hibernate.oneToMany.model.ItemsOIO; - -public class HibernateManyisOwningSide { - public static void main(String[] args) { - - Cart cart = new Cart(); - Cart cart2 = new Cart(); - - Items item1 = new Items(cart); - Items item2 = new Items(cart2); - Set itemsSet = new HashSet(); - itemsSet.add(item1); - itemsSet.add(item2); - - cart.setItems(itemsSet); - - - - 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(cart2); - session.save(item1); - session.save(item2); - // Commit transaction - tx.commit(); - session = sessionFactory.getCurrentSession(); - tx = session.beginTransaction(); - - item1 = (Items) session.get(Items.class, new Long(1)); - item2 = (Items) session.get(Items.class, new Long(2)); - tx.commit(); - - - System.out.println("item1 ID=" + item1.getId() + ", Foreign Key CartOIO ID=" + item1.getCart() - .getId()); - System.out.println("item2 ID=" + item2.getId() + ", Foreign Key CartOIO ID=" + item2.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(); - } - } - } -} diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneisOwningSide.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneIsOwningSide.java similarity index 50% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneisOwningSide.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneIsOwningSide.java index 0777664dd0..086e015ad1 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneisOwningSide.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneIsOwningSide.java @@ -9,57 +9,59 @@ import org.hibernate.Transaction; import com.baeldung.hibernate.oneToMany.config.HibernateAnnotationUtil; import com.baeldung.hibernate.oneToMany.model.CartOIO; -import com.baeldung.hibernate.oneToMany.model.ItemsOIO; +import com.baeldung.hibernate.oneToMany.model.ItemOIO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HibernateOneIsOwningSide { + + private static final Logger LOGGER = LoggerFactory.getLogger(HibernateOneIsOwningSide.class); -public class HibernateOneisOwningSide { public static void main(String[] args) { CartOIO cart = new CartOIO(); CartOIO cart2 = new CartOIO(); - ItemsOIO item1 = new ItemsOIO(cart); - ItemsOIO item2 = new ItemsOIO(cart2); - Set itemsSet = new HashSet(); + ItemOIO item1 = new ItemOIO(cart); + ItemOIO item2 = new ItemOIO(cart2); + Set itemsSet = new HashSet<>(); itemsSet.add(item1); itemsSet.add(item2); cart.setItems(itemsSet); - SessionFactory sessionFactory = null; - Session session = null; - Transaction tx = null; + SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory(); + Session session = sessionFactory.getCurrentSession(); + LOGGER.info("Session created"); + + Transaction tx; 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(cart2); session.save(item1); session.save(item2); + // Commit transaction tx.commit(); session = sessionFactory.getCurrentSession(); tx = session.beginTransaction(); - item1 = (ItemsOIO) session.get(ItemsOIO.class, new Long(1)); - item2 = (ItemsOIO) session.get(ItemsOIO.class, new Long(2)); + item1 = session.get(ItemOIO.class, 1L); + item2 = session.get(ItemOIO.class, 2L); tx.commit(); - System.out.println("item1 ID=" + item1.getId() + ", Foreign Key CartOIO ID=" + item1.getCartOIO() - .getId()); - System.out.println("item2 ID=" + item2.getId() + ", Foreign Key CartOIO ID=" + item2.getCartOIO() - .getId()); + LOGGER.info("item1 ID={}, Foreign Key CartOIO ID={}", item1.getId(), item1.getCartOIO().getId()); + LOGGER.info("item2 ID={}, Foreign Key CartOIO ID={}", item2.getId(), item2.getCartOIO().getId()); } catch (Exception e) { - System.out.println("Exception occured. " + e.getMessage()); - e.printStackTrace(); + LOGGER.error("Exception occurred", e); } finally { if (!sessionFactory.isClosed()) { - System.out.println("Closing SessionFactory"); + LOGGER.info("Closing SessionFactory"); sessionFactory.close(); } } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java index 2bc5e514f7..99df67b4a8 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java @@ -9,52 +9,53 @@ import org.hibernate.Transaction; import com.baeldung.hibernate.oneToMany.config.HibernateAnnotationUtil; import com.baeldung.hibernate.oneToMany.model.Cart; -import com.baeldung.hibernate.oneToMany.model.Items; +import com.baeldung.hibernate.oneToMany.model.Item; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class HibernateOneToManyAnnotationMain { + private static final Logger LOGGER = LoggerFactory.getLogger(HibernateOneToManyAnnotationMain.class); + public static void main(String[] args) { Cart cart = new Cart(); - Items item1 = new Items(cart); - Items item2 = new Items(cart); - Set itemsSet = new HashSet(); + Item item1 = new Item(cart); + Item item2 = new Item(cart); + Set itemsSet = new HashSet<>(); itemsSet.add(item1); itemsSet.add(item2); cart.setItems(itemsSet); - - SessionFactory sessionFactory = null; - Session session = null; - Transaction tx = null; + SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory(); + Session session = sessionFactory.getCurrentSession(); + LOGGER.info("Session created"); + try { - // Get Session - sessionFactory = HibernateAnnotationUtil.getSessionFactory(); - session = sessionFactory.getCurrentSession(); - System.out.println("Session created"); // start transaction - tx = session.beginTransaction(); + 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()); + + LOGGER.info("Cart ID={}", cart.getId()); + LOGGER.info("item1 ID={}, Foreign Key Cart ID={}", item1.getId(), item1.getCart().getId()); + LOGGER.info("item2 ID={}, Foreign Key Cart ID={}", item2.getId(), item2.getCart().getId()); } catch (Exception e) { - System.out.println("Exception occured. " + e.getMessage()); - e.printStackTrace(); + LOGGER.error("Exception occurred", e); } finally { if (!sessionFactory.isClosed()) { - System.out.println("Closing SessionFactory"); + LOGGER.info("Closing SessionFactory"); sessionFactory.close(); } } } - } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java index b8b991831e..53878af445 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java @@ -19,9 +19,8 @@ public class Cart { @Column(name = "cart_id") private long id; - @OneToMany(mappedBy = "cart") - private Set items; + private Set items; public long getId() { return id; @@ -31,12 +30,11 @@ public class Cart { this.id = id; } - - public Set getItems() { + public Set getItems() { return items; } - public void setItems(Set items) { + public void setItems(Set items) { this.items = items; } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/CartOIO.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/CartOIO.java index 8a5ed5e7a4..27b28a6753 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/CartOIO.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/CartOIO.java @@ -9,8 +9,6 @@ import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; - - @Entity @Table(name = "CARTOIO") public class CartOIO { @@ -21,7 +19,7 @@ public class CartOIO { @OneToMany @JoinColumn(name = "cart_id") // we need to duplicate the physical information - private Set items; + private Set items; public long getId() { return id; @@ -31,11 +29,11 @@ public class CartOIO { this.id = id; } - public Set getItems() { + public Set getItems() { return items; } - public void setItems(Set items) { + public void setItems(Set items) { this.items = items; } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Item.java similarity index 92% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Item.java index f63a4855b5..a055682d0d 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/Item.java @@ -11,23 +11,22 @@ import javax.persistence.Table; @Entity @Table(name = "ITEMS") -public class Items { +public class Item { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private long id; - @ManyToOne @JoinColumn(name = "cart_id", nullable = false) private Cart cart; // Hibernate requires no-args constructor - public Items() { + public Item() { } - public Items(Cart c) { + public Item(Cart c) { this.cart = c; } diff --git a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemsOIO.java b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemOIO.java similarity index 91% rename from persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemsOIO.java rename to persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemOIO.java index a3d6a796c5..baaf57b106 100644 --- a/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemsOIO.java +++ b/persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/oneToMany/model/ItemOIO.java @@ -10,7 +10,7 @@ import javax.persistence.Table; @Entity @Table(name = "ITEMSOIO") -public class ItemsOIO { +public class ItemOIO { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -21,10 +21,10 @@ public class ItemsOIO { private CartOIO cart; // Hibernate requires no-args constructor - public ItemsOIO() { + public ItemOIO() { } - public ItemsOIO(CartOIO c) { + public ItemOIO(CartOIO c) { this.cart = c; } diff --git a/persistence-modules/hibernate-annotations/src/main/resources/hibernate-annotation.cfg.xml b/persistence-modules/hibernate-annotations/src/main/resources/hibernate-annotation.cfg.xml new file mode 100644 index 0000000000..6e845b3781 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/resources/hibernate-annotation.cfg.xml @@ -0,0 +1,18 @@ + + + + + + org.h2.Driver + sa + + jdbc:h2:mem:spring_hibernate_one_to_many + + org.hibernate.dialect.H2Dialect + thread + true + create + + diff --git a/persistence-modules/hibernate-annotations/src/main/resources/log4j.xml b/persistence-modules/hibernate-annotations/src/main/resources/log4j.xml new file mode 100644 index 0000000000..bc2c24ea93 --- /dev/null +++ b/persistence-modules/hibernate-annotations/src/main/resources/log4j.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainIntegrationTest.java b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainIntegrationTest.java index 3bc5a6e12a..cb11df4212 100644 --- a/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainIntegrationTest.java +++ b/persistence-modules/hibernate-annotations/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainIntegrationTest.java @@ -1,26 +1,21 @@ package com.baeldung.hibernate.oneToMany.main; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.util.HashSet; -import java.util.Set; - +import com.baeldung.hibernate.oneToMany.config.HibernateAnnotationUtil; +import com.baeldung.hibernate.oneToMany.model.Cart; +import com.baeldung.hibernate.oneToMany.model.Item; import org.hibernate.Session; import org.hibernate.SessionFactory; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.cfg.Configuration; -import org.hibernate.dialect.H2Dialect; -import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.AfterClass; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -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.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; public class HibernateOneToManyAnnotationMainIntegrationTest { @@ -28,20 +23,9 @@ public class HibernateOneToManyAnnotationMainIntegrationTest { private Session session; - public HibernateOneToManyAnnotationMainIntegrationTest() { - } - @BeforeClass public static void beforeTests() { - Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class) - .setProperty("hibernate.dialect", H2Dialect.class.getName()) - .setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName()) - .setProperty("hibernate.connection.url", "jdbc:h2: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); + sessionFactory = HibernateAnnotationUtil.getSessionFactory(); } @Before @@ -52,32 +36,38 @@ public class HibernateOneToManyAnnotationMainIntegrationTest { @Test public void givenSession_checkIfDatabaseIsEmpty() { - Cart cart = (Cart) session.get(Cart.class, new Long(1)); + Cart cart = session.get(Cart.class, 1L); assertNull(cart); - } @Test public void givenSession_checkIfDatabaseIsPopulated_afterCommit() { Cart cart = new Cart(); - Set cartItems = new HashSet<>(); - cartItems = cart.getItems(); - Assert.assertNull(cartItems); - Items item1 = new Items(); + Set cartItems = cart.getItems(); + + assertNull(cartItems); + + Item item1 = new Item(); item1.setCart(cart); + assertNotNull(item1); - Set itemsSet = new HashSet(); - itemsSet.add(item1); - assertNotNull(itemsSet); - cart.setItems(itemsSet); + + Set itemSet = new HashSet<>(); + itemSet.add(item1); + + assertNotNull(itemSet); + cart.setItems(itemSet); + assertNotNull(cart); + session.persist(cart); session.getTransaction().commit(); session.close(); session = sessionFactory.openSession(); session.beginTransaction(); - cart = (Cart) session.get(Cart.class, new Long(1)); + cart = session.get(Cart.class, 1L); + assertNotNull(cart); } diff --git a/persistence-modules/hibernate-annotations/src/test/resources/log4j.xml b/persistence-modules/hibernate-annotations/src/test/resources/log4j.xml index 7e2895f1e0..bc2c24ea93 100644 --- a/persistence-modules/hibernate-annotations/src/test/resources/log4j.xml +++ b/persistence-modules/hibernate-annotations/src/test/resources/log4j.xml @@ -1,24 +1,31 @@ - + + + + + - + + + - \ No newline at end of file + + \ No newline at end of file diff --git a/persistence-modules/hibernate-annotations/src/test/resources/profile.png b/persistence-modules/hibernate-annotations/src/test/resources/profile.png deleted file mode 100644 index 1cd4e978b9..0000000000 Binary files a/persistence-modules/hibernate-annotations/src/test/resources/profile.png and /dev/null differ