I am working on a project that uses REST service(Jersey implementation) for some features like login,register etc.
At the login time i want to store the current user details in DB and that is successfully done with the below piece of code:-
Userlogin userlogin = new Userlogin(user,loginDateTime,null,ipAddress,tokenEnc,salt);
userloginBean.persist(userlogin);
userLoginId = userlogin.getId();
logger.log(Level.INFO, "userLoginId: "+userLoginId);
Now look at the third line of the code, i am trying to get the recently inserted id from the userlogin object. But it giving me null. In 4th line when i try to print it in log it show null.
I put some logs to check the execution and i found that the actual mysql query is executing after the 4th line.
So please tell me how can i sure that mysql has completed its query?
Also Explain me if possible ,why it is behaving like this as i use the same type code and i never get such situation.
I am using eclipse Kepler, Jsf 2.2
My project is Dynamic web project not maven.
For mysql query execution i am using Criteria API in DAO class.
I am persisting database using JPA.
Here is the persist method of my UserloginBean.java class:
public void persist(Userlogin transientInstance) {
log.log(Level.INFO, "persisting Userlogin instance");
try {
entityManager.persist(transientInstance);
log.log(Level.INFO, "persist successful");
} catch (RuntimeException re) {
log.log(Level.INFO, "persist failed", re);
throw re;
}
}
Here is some piece of my eclipse log:
2014-05-27T00:26:17.737+0530|INFO: persisting Userlogin instance
2014-05-27T00:26:17.813+0530|INFO: persist successful
2014-05-27T00:26:17.814+0530|INFO: userLoginId: null
2014-05-27T00:26:17.830+0530|FINE: INSERT INTO userlogin (help, ipaddress,
login, logout, token, user_id) VALUES (?, ?, ?, ?, ?, ?)
bind => [6 parameters bound]
2014-05-27T00:26:17.842+0530|FINE: SELECT LAST_INSERT_ID()
Thanks in advance. Sorry for my english mistake if i did any.
If you're using JPA, you'll need to call flush on the entityManager to see the values of fields that come from the database. e.g. generated id's, columns with default values, etc...