From 10a61af9151f8d7d1759afec002f95f503ebcb73 Mon Sep 17 00:00:00 2001 From: domingodelgado Date: Tue, 11 Dec 2018 16:11:19 +0100 Subject: [PATCH] Update test --- .../org/hibernate/bugs/JPAUnitTestCase.java | 41 +++++++++- .../org/hibernate/bugs/model/ContentDL.java | 69 +++++++++++++++++ .../org/hibernate/bugs/model/ProductDL.java | 77 +++++++++++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ContentDL.java create mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ProductDL.java diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index f36c69e3..26e494c6 100644 --- a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -1,9 +1,15 @@ package org.hibernate.bugs; +import java.util.HashMap; +import java.util.Map; + +import javax.persistence.EntityGraph; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; +import org.hibernate.bugs.model.ContentDL; +import org.hibernate.bugs.model.ProductDL; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -30,9 +36,42 @@ public void destroy() { @Test public void hhh123Test() throws Exception { EntityManager entityManager = entityManagerFactory.createEntityManager(); + + entityManager.getTransaction().begin(); + ProductDL productDL=new ProductDL() + .setName("Product 1"); + entityManager.persist(productDL); + + ContentDL content1DL=new ContentDL() + .setName("Content 1"); + content1DL.setProduct(productDL); + entityManager.persist(content1DL); + + ContentDL content2DL=new ContentDL() + .setName("Content 2"); + content2DL.setProduct(productDL); + entityManager.persist(content2DL); + entityManager.getTransaction().commit(); + entityManager.close(); + + entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + //Load product and store it in session cache + //Removing this line code works well due to "Product 1" dont be cached in session cache! + entityManager.find(ProductDL.class,productDL.getId()); + + //Relaod same product but now providing an EntityGraph with contents + EntityGraph eg=entityManager.createEntityGraph(ProductDL.class); + eg.addSubgraph("contents"); + Map props=new HashMap(); + props.put( + "javax.persistence.loadgraph", + eg); + productDL=entityManager.find(ProductDL.class,productDL.getId(),props); entityManager.getTransaction().commit(); entityManager.close(); + + productDL.getContents(); } } diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ContentDL.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ContentDL.java new file mode 100644 index 00000000..f0a09b0e --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ContentDL.java @@ -0,0 +1,69 @@ +package org.hibernate.bugs.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class ContentDL { + + private Integer id; + private String name; + private ProductDL product; + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + public Integer getId() { + return id; + } + public ContentDL setId(Integer id) { + this.id = id; + return this; + } + public String getName() { + return name; + } + public ContentDL setName(String name) { + this.name = name; + return this; + } + + @ManyToOne + public ProductDL getProduct() { + return product; + } + public void setProduct(ProductDL product) { + this.product = product; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ContentDL other = (ContentDL) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "ContentDL [" + (id != null ? "id=" + id + ", " : "") + (name != null ? "name=" + name : "") + "]"; + } +} \ No newline at end of file diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ProductDL.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ProductDL.java new file mode 100644 index 00000000..dcfe1d34 --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/model/ProductDL.java @@ -0,0 +1,77 @@ +package org.hibernate.bugs.model; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class ProductDL { + + private Integer id; + private String name; + private Set contents; + + public ProductDL() { + contents=new HashSet<>(); + } + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + public Integer getId() { + return id; + } + public ProductDL setId(Integer id) { + this.id = id; + return this; + } + public String getName() { + return name; + } + public ProductDL setName(String name) { + this.name = name; + return this; + } + + @OneToMany(mappedBy="product") + public Set getContents() { + return contents; + } + public ProductDL setContents(Set contents) { + this.contents = contents; + return this; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ProductDL other = (ProductDL) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "ProductDL [" + (id != null ? "id=" + id + ", " : "") + (name != null ? "name=" + name : "") + "]"; + } +} \ No newline at end of file