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.
Java code DatabaseObject.java
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;
}
Using DatabaseObject
String sql = "INSERT INTO user(emailAddress, password) VALUES(:emailAddress, :password)";
UserVO valueObject = new UserVO("test@email.com", "pass1234");
databaseObject.insert(sql, valueObject);
public BigInteger insert(String sql, Object valueObject) throws Exception
{
Session session = Entitlement.getSessionFactory().openSession();
Transaction txn = session.beginTransaction();
session.createSQLQuery(sql)
.setProperties(valueObject)
.executeUpdate();
BigInteger result = session.createSQLQuery("SELECT LAST_INSERT_ID()")
.uniqueResult();
txn.commit();
session.close();
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.