Announcement Announcement Module
No announcement yet.
Newbie Hibernate question Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie Hibernate question

    I'm hoping somebody can confirm whether my interpretation of how to use Hibernate and POJO instances in DAO implementation classes is correct.

    For 'add' style methods that post new records to the underlying table then in the related 'add' method I should NOT be persisting the POJO object passed in as the input parameter, but instead creating a local instance and copying the data from the input parameter to the locally created instance and persisting the locally created instance, like...

    public void add(Entity anEntity) {
       // create local instance
       Entity aLocal = new Entity();
       // move data from parameter instance to local instance
       // persist local instance
    Is this correct?

    Equally for retrieving data.. should I be passing back copies of the retrieved instances, like...
    public Entity[] getAll() {
          List aList = getHibernateTemplate().find("from Entity");
          Entity[] anArray = new Entity[aList.size()];
          Iterator anIterator = aList.iterator();
          int i=0;
          while (anIterator.hasNext()) {
             anArray[i] = new Entity();
          return anArray;
    I'm assuming the point of both implementations is to shield the 'Entity' instances being cached by Hibernate from any data setting happening in the business tier, right?
    Last edited by davout; Feb 7th, 2007, 12:19 PM.

  • #2
    Well it depends. If you want to expose the objects directly to the presentation, then you don't really want to be doing that. If you want to send copies up, then just evict the object from the Session.