-->
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.  [ 3 posts ] 
Author Message
 Post subject: Check the existence of the enity
PostPosted: Wed Apr 05, 2006 9:04 am 
Newbie

Joined: Thu Nov 17, 2005 12:59 pm
Posts: 18
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.2

I would like to check, if the entity with the given ID exist in the DB. From documentation session.load() should throw an exception and session.get() should return null if there is no the entity with given ID in the DB. Also the doc says, that the session.get() returns:

"If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy."

A I see from my tests, the session.load() also return the proxy in this case.

I use the SpringDAOSupport, and as I see, spring doesn create the new hibernate session while calling each DAO method. Therefore I see only one solution:
select count(*) from MyObject where myId=id
and check this value.

The my question is: is the another way to check the entity existance?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 10:51 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Maybe I'm missing something from your question, but Session.get() will work fine for checking if a record exists in the db. If it returns null, it doesn't exist, if it returns non-null (either an instance or proxy), then the records does exist.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 3:15 am 
Newbie

Joined: Thu Nov 17, 2005 12:59 pm
Posts: 18
nathanmoon, as I noted above, the Session javadoc says fo the .get() method:
"If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy."

And as I see from my unit tests, the proxy was created also for the non-existent id. For example, session.get(MyClass, -1) retrive the proxy, with id=-1. My code below:
Code:
    public void updateCatalogInfo(final Catalog catalog)
        throws NoSuchCatalogException, CatalogException {

        final Integer catalogId = catalog.getCatalogId();
        try {

            HibernateTemplate template = getHibernateTemplate();
            Catalog persistantedCatalog
                = (Catalog) template.get(Catalog.class, catalogId);
            if (null == persistantedCatalog) {
               
                //this is unsearchable in the my tests, even for the id=-1
                throw new NoSuchCatalogException("There is no catalog "
                    + "with id = '" + catalogId + "'.");
            }
            template.update(catalog);
        }
        catch (DataAccessException he) {

            logger.error(he);
            throw new CatalogException("Can't update the catalog with id = '"
                + catalogId + "'.", he);
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.