Hi All,
I'm trying to implement a hierarchical data model following
the "closure table" pattern described in this awesome slide (page 40)
http://www.slideshare.net/billkarwin/models-for-hierarchical-data.
I've started with a simple pojo (id, desc) and a basic DAO implementation:
Code:
public void insert(Category cat) {
Session session = this.getSessionFactory().getCurrentSession();
session.save(cat);
}
public List<Category> loadAll() {
return (List<Category>) this.getSessionFactory().getCurrentSession().createQuery("from Category ").list();
}
After that, I was trying to add to my Category the "path":
Code:
public List<Integer> getParents();
public List<Integer> getChildren();
These are the two statements I have to perform in order to load the full path,
but I can't figure out to do it inside the loadAll() method
Code:
//parents
SELECT c.id FROM category c JOIN category_tree t ON (c.id = t.ancestor) WHERE t.descendant = ? and t.depth > 0
//children
SELECT c.id FROM category c JOIN category_tree t ON (c.id = t.descendant) WHERE t.ancestor = ? and depth=1
I'm using 4.1.0.Final with annotations
Thanks!