-->
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.  [ 4 posts ] 
Author Message
 Post subject: Performance of em.find() vs em.getResultList()
PostPosted: Fri Oct 18, 2013 8:03 pm 
Newbie

Joined: Fri Oct 18, 2013 7:43 pm
Posts: 2
Hi all

I tidied some of our JPA code to make the code more readable and now my colleague's annoyed because he says the new code is less performant. It's a very simple scenario and I'm not convinced he's right.

I replaced a named query with find(). The named query gets an account using its primary key, like this:
Code:
@NamedQueries(value = { @NamedQuery(name = "account.findAccount", query = "SELECT a FROM Account a WHERE a.id=:accountId") })

The code that triggers the query is something like:
Code:
TypedQuery<Account> q = em.createNamedQuery("account.findAccount", Account.class);
q.setParameter("accountId", accountId);
List<Account> result = q.getResultList();
if (result.size() == 1) {
    return result.get(0);
} else if (result.isEmpty()) {
    return null;
} else {
    throw new EJBException(...);
}

I replaced all of the above with:
Code:
return em.find(Account.class, accountId);

Is the named query in this case any faster than find()? In terms of initialising, executing, caching results, etc? As far as I'm aware find() is designed to do exactly the above and I'd imagine it's been optimised as such.

Can any of your Hibernate/JPA experts shed any light on this? We're running Wildfly 8 alpha4.

Thanks all

Andrew.


Top
 Profile  
 
 Post subject: Re: Performance of em.find() vs em.getResultList()
PostPosted: Sun Oct 20, 2013 9:41 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
"performance" is hard to define and you should be prepared to measure this according to what your goals are. That would surely benefit discussion with a colleague as you can then argue on the validity of the test :-)

If I had to guess, I would agree with your cleanup as it's supposed to be way more efficient.

If this operation is a performance concern make sure you enable 2nd level caching.

You could also consider
Code:
javax.persistence.EntityManager.getReference(Class<T>, Object)

assuming it's safe for your use case:
careful as it throws an error when accessing the proxy if it turns out it's not actually existing, so if you're not sure of it to exist or if that would not be an error, use find();

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


Top
 Profile  
 
 Post subject: Re: Performance of em.find() vs em.getResultList()
PostPosted: Tue Oct 22, 2013 7:38 am 
Newbie

Joined: Fri Oct 18, 2013 7:43 pm
Posts: 2
Thanks for the info, Sanne!

I've offered to revert my change just to avoid a fight but it's good to know that find() is mean to be more efficient. I think my colleague's concern is that named queries are compiled into SQL at compile time while find() generates its SQL at runtime. I wonder if there's any truth in that.

Thanks

Andrew.


Top
 Profile  
 
 Post subject: Re: Performance of em.find() vs em.getResultList()
PostPosted: Tue Oct 22, 2013 11:35 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I think my colleague's concern is that named queries are compiled into SQL at compile time while find() generates its SQL at runtime.

Hibernate will create most SQL it needs for common operations at boot time, and this will be registered as a prepared statement.
There certainly is a find() operation generated as such a prepared statement, so it degenerates in a remote function call.

There is no magic at compile time, unless you instrument your classes at compile time.

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


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