I have tree structures that are stored in a database table. A table can store several trees. I need a query that will return all nodes in a single tree . I used the following sites as resources, but queries in them will load all the nodes for all trees in the PersistenceContext (will you load the entire table?). I do not want to do this, I only want to load one tree. How to achieve this?
I am using JPA 2 with Hibernate as a provider.
OpenJPA 1.2.x select a tree structure using JPQL
http://www.tikalk.com/java/load-a-tree-with-jpa-and-hibernate#comment-1821
[UPDATE] Based on the assumption from @bennidi, I wonder if I can use something like this:
@Entity
public class Node {
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "rootId")
private Node root;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parentId")
private Node parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("name")
private List<Node> children = new LinkedList<Node>();
}
JPQL query:
select distinct n from Node n left join fetch n.children where n.rootId = ROOT_ID
I have a question, although I have to create a second table to support the relationship of the parent child, for example, in this article or it will be good if I just use the self-regulation table. Moving from the first seems to require a bit more maintenance and probably a bit more complex queries. Is it worth it? I am not sure what problems the article is facing.
source
share