title: How to query a database using Hibernate?
body: <p>I understand some might simply answer this question with "Why didn't you just Google it"... But I did, and the more I researched this the more confused I got. I'm trying to query my database with Hibernate, the query has a 'where' clause. </p>
  
  <p>Now creating a database entry is easy enough, in the case where I have a 'User' class, I simply do this:</p>
  
  <pre><code>// Gets a new session
  Session session = HibernateUtil.getSessionFactory().openSession();
  session.beginTransaction();
  
  // Creates a new User object
  User user = new User("John", "p@55w0rd*", "john@doe.com");
  
  // Save and commit
  session.save(user);
  session.getTransaction().commit();
  </code></pre>
  
  <p>But what do I do when I what to for instance</p>
  
  <blockquote>
    <p>select * from Users where id = '3';</p>
  </blockquote>
  
  <p>My Google searches pointed to something called HQL, which makes me wonder why I couldn't of just used straight JDBC then. Also it doesn't seem very object oriented. And then there's something like</p>
  
  <blockquote>
    <p>session.createCriteria(....... </p>
  </blockquote>
  
  <p>But I'm not sure how to use this.. Any help? Thanks guys.</p>