I am trying to use EntityGraph to avoid using fetch=FetchType.EAGER:
EntityGraph<?> graph = entityManager.getEntityGraph("parent.hierarchy");
Map<String, Object> hints = new HashMap<>();
hints.put("jakarta.persistence.fetchgraph", graph);
return entityManager.find(ParentEntity.class, id, hints); // return everything
I expected the find to return all the relationships and my Entity structure look something like this (simplified):
@Entity
@Table(name = "PARENT_TABLE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "PARENT_TYPE_ID", discriminatorType = DiscriminatorType.INTEGER)
@NamedEntityGraph(name="parent.hierarchy",
attributeNodes = {
@NamedAttributeNode(value="children", subgraph="parent.child.hierarchy"),
},
subgraphs = {
@NamedSubgraph(
name = "parent.child.hierarchy",
attributeNodes = {
@NamedAttributeNode(value="grandchildren", subgraph = "parent.grandchildren.hierarchy"),
@NamedAttributeNode("childSubscribers")
}
)
}
)
public abstract class ParentEntity implements Serializable {
private Long parentTypeId;
private List<ChildEntity> children;
@Column(name = "PARENT_TYPE_ID", insertable=false, updatable=false)
public Long getParentTypeId() {
return parentTypeId;
}
public void setParentTypeId(Long parentTypeId) {
this.parentTypeId = parentTypeId;
}
@OneToMany(targetEntity = ChildEntity.class, orphanRemoval=true, cascade=CascadeType.ALL)
@JoinColumn(name="POLICIES_ID")
public List<ChildEntity> getChilds() {
return children;
}
public void setChilds(List<ChildEntity> children) {
this.children = children;
}
...
}
@Entity
@Table(name="CONCREATE_PARENT_TABLE_A")
@DiscriminatorValue(100L)
public class ConcreateParentAEntity extends ParentEntity {
...
}
@Entity
@Table(name="CONCREATE_PARENT_TABLE_B")
@DiscriminatorValue(101L)
public class ConcreateParentBEntity extends ParentEntity {
...
}
@Entity
@Table(name="CHILD_TABLE")
@DiscriminatorColumn(name="CHILD_SUBTYPE", discriminatorType=DiscriminatorType.CHAR)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue("B") // default // #hibernate
public class ChildEntity implements Serializable {
private Character subtype;
private List<GrandchildEntity> grandchildren;
@Column(name="CHILD_SUBTYPE", insertable=false, updatable=false)
public Character getSubtype() {
return subtype;
}
public void setSubtype(Character subtype) {
this.subtype = subtype;
}
@OneToMany(targetEntity=GrandchildEntity.class, cascade=CascadeType.ALL,orphanRemoval=true)
@JoinColumn(name="CHILD_ID")
public List<GrandchildEntity> getGrandchildren() {
return grandchildren;
}
public void setGrandchildren(List<GrandchildEntity> grandchildren) {
this.grandchildren = grandchildren;
}
...
}
@Entity
@Table(name="CONCRETE_CHILD_A")
@DiscriminatorValue("A")
@PrimaryKeyJoinColumn(name="CHILD_ID")
public class ConcreteChildAEntity extends ChildEntity {
...
}
@Entity
@Table(name="CONCRETE_CHILD_B")
@DiscriminatorValue("B")
@PrimaryKeyJoinColumn(name="CHILD_ID")
public class ConcreteChildBEntity extends ChildEntity {
...
}
But the entity.find() with the entity graph does not seems to be fetching the relations, children and grandchildren! What might be wrong? How might I debug or correct the issue? Application is running under Wildfly 30.0.0-Final
Thanks!
Test with:
See errors or what is it returns and go from there divide and conquer.