title: Hibernate custom sql to get last insert id
body: <p>how can I return auto-generate id of a newly inserted row using custom sql and value object method (without save/update etc.). Id's (Identity Fields) are handled by the database server.</p>
  
  <p>Java code DatabaseObject.java</p>
  
  <pre><code>public int insert(String sql, Object valueObject) throws Exception {
      Session session = Entitlement.getSessionFactory().openSession();
      session.beginTransaction();
      int result = session.createSQLQuery(sql)
              .setProperties(valueObject).executeUpdate();
      session.getTransaction().commit();
      session.close();
      return result;
  }
  </code></pre>
  
  <p>Using DatabaseObject</p>
  
  <pre><code>String sql = "INSERT INTO user(emailAddress, password) VALUES(:emailAddress, :password)";
  UserVO valueObject = new UserVO("test@email.com", "pass1234");
  databaseObject.insert(sql, valueObject);
  </code></pre>