hibernate createQuery vs get

hibernate, java

Solution

First, it's much quicker to type, is much more readable, and expresses the intent clearly: get an entity by its ID. And it's basically impossible to make an error, whereas you could have a typo in your HQL query.

Regarding performance, the main advantage is that it executes a select statement only if the entity is not in the session cache yet. An HQL query will be executed every time. And if you have a second-level cache, `get()` will avoid executing a query completely if the entity is already in the second-level cache.

Problem

What advantages does `get` have over `createQuery`? I can see there might be a slight performance improvement in not having to parse the HQL, but is there any other major advantage to using get over createQuery?

Original source

Related problems