hi,
how can i prevent entityManager from calling a flush when it goes out of context in a bean?
for instance, in my AuthenticatorAction, i have the following code:
public boolean authenticate() { try { User u = (User) em.createQuery( "from User where username = :username").setParameter( "username", identity.getUsername()) .getSingleResult(); if (!compareHash(u.getHashedPassword(), identity.getPassword())) { FacesMessages.instance() .add("Wrong password for #{identity.username}"); return false; } loggedInUser = u; return true; } catch (NoResultException ex) { FacesMessages.instance() .add("#{identity.username} does not exist!"); return false; } }
this always calls an update on the User being queried. i was having the same problem occur in some validator beans that i have as well, which i prevented by adding
.setHint("org.hibernate.readOnly", new Boolean(true))
.createQuery()
is there any other way to prevent this db round-trip?
regards,
sb
try ((Session)em.getDelegate()).setFlushMode(FlushMode.MANUAL);
or
@Begin(flushMode = "MANUAL")
if you can.