-->
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.  [ 6 posts ] 
Author Message
 Post subject: find by primary key
PostPosted: Thu Jun 24, 2004 7:55 am 
Newbie

Joined: Thu Jun 24, 2004 7:43 am
Posts: 3
Hello,

I am currently working on migration of persistence layer from CMP to BMP using Hibernate as DAO's.

My BMP ejbFindByPrimaryKey method looks like this:

public BeanKey ejbFindByPrimaryKey(BeanKey key) throws FinderException {
try {
BeanDAO dao = (BeanDAO) DAOFactory.getInstance().getDAO(this);
key = dao.findByPrimaryKey(key);
return key;
} catch (ObjectNotFoundDAOException onfde) {
throw new ObjectNotFoundException(onfde.getMessage());
} catch(DAOException ex) {
throw new FinderException(ex.toString());
}
}

while the DAO's findByPrimaryKey method is

public BeanKey findByPrimaryKey(BeanKey pk) throws DAOException {
Session session = null;
try {
session = openSession();
session.load(BeanBean.class, pk.something);
return pk;
} catch (ObjectNotFoundException onfe) {
throw new ObjectNotFoundDAOException("not found");
} catch (HibernateException he) {
throw new DAOException("error");
} finally {
closeSession(session);
}
}

My question is: Is there any other (faster) way of performing the function of findByPrimaryKey in hibernate, that wouldn't try to load the object into memory, instead of it only check whether the object with given id exists?

_________________
---
Best Regards,
Tomasz Luczynski


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 12:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
use session.get() and check for null if you don't want a proxy. How else should it work faster?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 27, 2004 5:30 am 
Newbie

Joined: Thu Jun 24, 2004 7:43 am
Posts: 3
Hello,

As I userstand correctly from my experience, Session.get() will load the object if it exists. in this case it will result in loading the object twice:

1. DAO.findByPrimaryKey loads object, returns object primary key, discards the loaded object

2. beans ejbLoad() loads object again

In findByPrimaryKey all I need to know is if the specified object exists and not load them and waste precious time. I thought using JDBC query just to check if the specified id exists would be fastest way for findByPrimaryKey, but perhaps there is a better way to do it in Hibernate? Like lazy loading of objects queried for or something?

_________________
---
Best Regards,
Tomasz Luczynski


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 27, 2004 8:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
how would you just check for existance with JDBC? You can do just the same with HQL like "select a.id from Someclass a where a.id=?" if you mean something like that. I don't actually get what you suggest.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 1:54 am 
Newbie

Joined: Thu Jun 24, 2004 7:43 am
Posts: 3
Yes, I meant to select only for id not whole row of data with jdbc. If I do such query with HQL, would the result be loaded into pojo, making all the rest of the fields null except id?

_________________
---
Best Regards,
Tomasz Luczynski


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 8:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Read the documentation.


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