I need a way to fetch the last id of an entity in database, let's say for example
Product entity:
I try this but its not working:
public int lastInsertedId() {
try {
if (!session.isOpen())
session = DatabaseUtil.getSessionFactory().openSession();
session.beginTransaction();
Query query = session.createSQLQuery("select last_value from purchase_idpurchase_seq ");
int lastid = query.getFirstResult();
session.getTransaction().commit();
session.close();
return lastid;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}