users@glassfish.java.net

EntityManager.refresh() How to save a DB trip?

From: <glassfish_at_javadesktop.org>
Date: Thu, 21 Aug 2008 21:47:58 PDT

Why can't I get fresh data from the first query?

@Stateless
public class AccessManagerBean implements AccessManagerRemote {

    @PersistenceContext(unitName="xyz-ejbPU")
    private EntityManager em;

    Users user = new Users();

    public User getUser(String uName){

         return (User)em.createNamedQuery("User.findByUserName").setParameter("userName", uName).getSingleResult();

    }

Why do I need to do a refresh() and add a second trip to the database in order to display up-to-date data?

    public User getUser(String uName){

         user = (User)em.createNamedQuery("User.findByUserName").setParameter("userName", uName).getSingleResult(); // First trip
         em.refresh(user); // Second trip
         return user;
    }

Am I missing something?

Thanks for your help.
[Message sent by forum member 'javaway' (javaway)]

http://forums.java.net/jive/thread.jspa?messageID=294713