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.