Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between em.find and em.createQuery

Tags:

java

jpa

openjpa

em.find(Enity.classs, Id)

em.createQuery("SELECT e From Entity e WHERE e.Id=:id")

Are they equivalent? If not, How are they different?

like image 606
SQC Avatar asked Mar 11 '26 14:03

SQC


1 Answers

The former might be faster because JPA provider does not have to parse the JPA query. But the resulting SQL should be exactly the same.

Also I think some providers might treat them differently with regards to caching - caching by key vs. query caching.

Finally the API is much more pleasant with find() - it either returns single entry or null. The latter either returns exactly one result (getSingleResult() - or throws) or returns a list (getResultList()).

like image 108
Tomasz Nurkiewicz Avatar answered Mar 14 '26 04:03

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!