These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: [n00b-Q] load collection twice returns different objects
PostPosted: Mon Nov 30, 2009 10:58 am 
Newbie

Joined: Mon Nov 30, 2009 10:31 am
Posts: 2
i probably don't understand the documentation, but i thought that whenever you ask hibernate to fetch an object from the DB it checks whether this object is already loaded in memory, and if it already is, it returns the already loaded object, right?
If i understand it correctly, that is certainly valid within the same session...

Sessions have a lifetime that is about the same as a transaction, right?

I have the following code to load all instances of the tyê Onderwerp into memory (very short list, will never grow beyond 200 objects)
Code:
    public List<Onderwerp> getAllOnderwerpen(){
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Onderwerp.class);
        List<Onderwerp> onderwerpList = criteria.list();
        return onderwerpList;
    }


if i load this in session1 i get a certain collection.
if i load this in session1 i get a different collection.

i want to delete some object from the DB.. how to proceed? I get a NonUniqueObjectException when calling
Code:
    public Boolean deleteOnderwerp(Onderwerp onderwerp) {
        Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
        try{

            sessionFactory.getCurrentSession().delete(onderwerp);
            tx.commit();
            return true;
        }
        catch(HibernateException hibex){
            tx.rollback();
            log.debug(hibex.getMessage(),hibex);
            return false;
        }
    }


Should i
1. Use some caching of my own inbetween, like:

Code:
private List<Onderwerp> myOnderwerpList;

    public List<Onderwerp> getAllOnderwerpen(){
        if (myOnderwerpList==null){
          Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Onderwerp.class);
          onderwerpList = criteria.list();
        }
        return onderwerpList;
    }


thereby also modifying this collection when deleting objects?

2. Invoke some arcane hibernate magic ?
Like.. uhm... enlighten me please...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.