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;
}
}
Try to use something like this:
And to get the value of
last_valuetry this:I suppose you have an entity
purchase_idpurchase_seqTry to take a look at this site