The following link: http://www.tikalk.com/java/load- a- tree- with- jpa-and-hibernate describes how to load a Recursively structured Composite Object pattern from a single table. Hibernate generates this object quite well, and does a fine job with both Lazy as well as Eager loading of the children - If however you try to have an Entity with the following bi-directional relationship:
Public Class CompositeElement { ... @ManyToOne @JoinColumn(name = "parent_id") public CompositeElement getParent() { return parent; }
@OneToMany(mappedBy = "parent", targetEntity = CompositeElement.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL) public List<CompositeElement> getChildren() { return children; }
but try to override the fetch behavior in a particular use case for example using a Criteria:
criteria.setFetchMode("children", FetchMode.JOIN);
The query which is generated is not correct for postgres: from CompositeElement compositee0_ left outer join CompositeElement compositee2_ on compositee1_.parent_id=compositee2_.id
Which will only go one level deep into the recursive structure.
This should generate a with recursion query for postgres.
|