Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: [Help][newbie] Question about loading list
PostPosted: Thu Aug 09, 2012 3:34 am 
Newbie

Joined: Wed Aug 08, 2012 6:51 am
Posts: 2
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!


Top
 Profile  
 
 Post subject: Re: [Help][newbie] Question about loading list
PostPosted: Thu Aug 09, 2012 8:33 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
Hi,
what is your goal?

after loading each Category, doing category.getParents() or getChildren() will just work without any need for explicit loading, as long as you don't close the Session. The idea behind Hibernate is to keep the Session open around all code blocks which deal with the data manipulation.

If what you want is to make sure that parents and children are loaded with the same SQL statement (not a functional difference just a performance impact) then you want to look into fetch options, either on the mapping or specifying a fetch join in the query; I'm not suggesting that however.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [Help][newbie] Question about loading list
PostPosted: Fri Aug 10, 2012 1:04 pm 
Newbie

Joined: Wed Aug 08, 2012 6:51 am
Posts: 2
Hi Sanne,

being new to hibernate and jpa sometimes I find myself thinking in jdbc :)

thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.