I have the code below which will query an entity and set a random number:
EntityManager em = EMF.get().createEntityManager();
Profile user = null;
Query q = null;
try{
q = em.createNamedQuery("Profile.getRandomProfile");
q.setParameter("random", Math.random());
q.setMaxResults(1);
user = (Profile) q.getSingleResult();
user.setRandom( Math.random() );
} catch(NoResultException ex){
ex.printStackTrace();
}
I thought doing so will update random property of profile. Why isn’t it updated? How to update the random property?
You need to begin and commit a transaction around your changes.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Transactions