[jboss-user] [JBoss Seam] - Update entity in authenticate method

twocoasttb do-not-reply at jboss.com
Fri Apr 20 13:17:20 EDT 2007


What is the correct way to update an entity in my authenticate method (called from Identity)?  I want to update the user entity with a 'last connected' date.  As far as I can tell, I have to handle the transaction manually, like this:

@Name("authenticator")
  | public class Authenticator {
  | 
  |   @In
  |   EntityManager entityManager;
  | 
  |   @Out(scope=ScopeType.SESSION, required=false)
  |   SiteUser currentUser;
  | 
  |   @In(create=true) @Out
  |   SessionPreferences prefs;
  | 
  |   public boolean authenticate() {
  |     Query q = entityManager.createQuery("from SiteUser su where upper(username) = upper(:username) and password = :password and active = true");
  |     q.setParameter("username", Identity.instance().getUsername());
  |     q.setParameter("password", Identity.instance().getPassword());
  |     if (q.getResultList().size() == 1) {
  |       currentUser = (SiteUser)q.getSingleResult();
  |       prefs.setCurrentOrganization(currentUser.getOrganization());
  |       Identity.instance().addRole("admin");
  | 
  |       try {
  |         UserTransaction utx = Transactions.getUserTransaction();
  |         utx.begin();
  |         entityManager.joinTransaction();
  |         currentUser.setLastConnected(new java.util.Date());
  |         utx.commit();
  |       } catch (Exception e) {
  |         log.error("Exception updating user: " + e.getMessage());
  |       }
  | 
  |       return true;
  |     }
  |     return false;
  |   }
  | }

Is there a better way to do it?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4039359#4039359

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4039359



More information about the jboss-user mailing list