package example.ejb.impl; import example.ejb.interfaces.UserCredentialManager; import example.entity.UserCredential; import javax.ejb.*; import javax.persistence.*; @Stateless @Remote public class UserCredentialManagerBean implements UserCredentialManager { @PersistenceContext private EntityManager em; public void createUser(UserCredential uc) { // em.persist makes the new object as persistent and managed. em.persist(uc); } public void authenticate(String name, String password) { UserCredential uc = em.find(UserCredential.class, name); // em.find returns null if not found, hence null check. if (uc == null || !uc.isMatchingPassword(password)) { throw new EJBException("Incorrect user name or password"); } } public void removeUser(String name) { UserCredential uc = em.find(UserCredential.class, name); // em.find returns null if not found, hence null check. if (uc != null) { em.remove(uc); } else { throw new EJBException("No such user"); } } }