I want to fetch the last inserted value\'s id in Hibernate.
After search:
Long lastId = ((Long) session.createSQLQuery(\"SELECT LAST_INSERT_ID()\").
Since the return type of uniqueResult()
is BigInteger
and not Long
, you should do it like this:
long lastId = session.createSQLQuery("SELECT LAST_INSERT_ID()")
.uniqueResult() // this returns a BigInteger
.longValue(); // this will convert it to a long value
The method uniqueResult()
only returns a BigInteger because of your query SELECT LAST_INSERT_ID()
.