Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Do you mean a HQL query? Well, a typical query on your <code>Leavetable</code> entity would looks like this:</p> <pre><code>Query q = session.createQuery("from Leavetable t where t.someField = :value"); q.setParameter("value", foo); List&lt;Leavetable&gt; results = q.list(); </code></pre> <hr> <p>However, if you just want to retrieve an entity by identifier, see <a href="http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/Session.html#load(java.lang.Class,%20java.io.Serializable)" rel="nofollow noreferrer"><code>Session#load()</code></a> or <a href="http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/Session.html#get%28java.lang.Class,%20java.io.Serializable%29" rel="nofollow noreferrer"><code>Session#get()</code></a>. I don't want to make things too much confusing but while both methods are similar, there is an important difference between both of them. Quoting the <a href="https://forum.hibernate.org/viewtopic.php?p=2387456" rel="nofollow noreferrer">Hibernate Forums</a>:</p> <blockquote> <h3>Retrieving objects by identifier</h3> <p>The following Hibernate code snippet retrieves a User object from the database:</p> <pre><code>User user = (User) session.get(User.class, userID); </code></pre> <p>The <code>get()</code> method is special because the identifier uniquely identifies a single instance of a class. Hence it’s common for applications to use the identifier as a convenient handle to a persistent object. Retrieval by identifier can use the cache when retrieving an object, avoiding a database hit if the object is already cached. Hibernate also provides a <code>load()</code> method:</p> <pre><code>User user = (User) session.load(User.class, userID); </code></pre> <p>The load() method is older; get() was added to Hibernate’s API due to user request. The difference is trivial:</p> <p>If <code>load()</code> can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The <code>get()</code> method returns <code>null</code> if the object can’t be found.</p> <p>The <code>load()</code> method may return a proxy instead of a real persistent instance. A proxy is a placeholder that triggers the loading of the real object when it’s accessed for the first time; we discuss proxies later in this section. On the other hand, <code>get()</code> never returns a proxy.</p> <p><strong>Choosing between <code>get()</code> and <code>load()</code> is easy:</strong> If you’re certain the persistent object exists, and nonexistence would be considered exceptional, <code>load()</code> is a good option. If you aren’t certain there is a persistent instance with the given identifier, use <code>get()</code> and test the return value to see if it’s null. Using <code>load()</code> has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So <code>load()</code> might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed.</p> <p>Of course, retrieving an object by identifier isn’t as flexible as using arbitrary queries.</p> </blockquote> <p>See also the Hibernate Documentation (links below).</p> <h3>Reference</h3> <ul> <li>Hibernate Core Reference Guide <ul> <li><a href="http://docs.jboss.org/hibernate/core/3.5/reference/en/html/objectstate.html#objectstate-loading" rel="nofollow noreferrer">10.3. Loading an object</a></li> <li><a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html" rel="nofollow noreferrer">Chapter 14. HQL: The Hibernate Query Language</a> </li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload