Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-13152 Error loading entity with Entity Graph when entity already stored in session cache. #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ProductDL> eg=entityManager.createEntityGraph(ProductDL.class);
eg.addSubgraph("contents");
Map<String,Object> props=new HashMap<String,Object>();
props.put(
"javax.persistence.loadgraph",
eg);
productDL=entityManager.find(ProductDL.class,productDL.getId(),props);
entityManager.getTransaction().commit();
entityManager.close();

productDL.getContents();
}
}
Original file line number Diff line number Diff line change
@@ -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 : "") + "]";
}
}
Original file line number Diff line number Diff line change
@@ -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<ContentDL> 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<ContentDL> getContents() {
return contents;
}
public ProductDL setContents(Set<ContentDL> 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 : "") + "]";
}
}