title: Last inserted Id in Java Hibernate
body: <p>How do we get the last inserted value? Below is my function which inserts values. But I want to fetch last inserted <code>Id</code> which is auto incremented. I am using PostgreSQL</p>
  
  <pre><code>@Override
  public int executeInsertSQLSelect(String sqlQuery) throws HibernateException {
  
      SQLQuery query = getCurrentSession().createSQLQuery(sqlQuery);
  
      logger.debug("SQL query to be executed is : " + query);
  
      int noOfRecordsUpdatedOrDeleted;
  
      try {
          noOfRecordsUpdatedOrDeleted = query.executeUpdate();
  
  
      } catch (HibernateException hbExp) {
          logger.error(String.format(
                  "Error occured while executing query without Scalar"
                          + " mapping - %s ", query.getQueryString()), hbExp);
          throw hbExp;
      }
      return noOfRecordsUpdatedOrDeleted;
  }
  </code></pre>