我似乎无法捕获java ee 6项目中的NoResultException或EJBException等异常。有什么我做错了吗?我在EJB容器外捕获了异常,但这是我第一次使用EJB。感谢。
@Stateless
public class UserEJB {
@PersistenceContext
EntityManager em;
public String getUserName(User user) {
return user.getName();
}
public User fetchUserByEmail(String email) {
User user = em.createNamedQuery("User.findByEmail", User.class).setParameter("email", email).getSingleResult();
return user;
}
public User fetchUserById(int id) {
return em.createNamedQuery("User.findByUserId", User.class).setParameter("userId", id).getSingleResult();
}
public List<User> fetchAllUsers() {
return em.createNamedQuery("User.findAll", User.class).getResultList();
}
}
@Named(value = "userController")
@RequestScoped
public class UserController {
private User user = new User();
@EJB
UserEJB userEJB;
@Inject
SecurityController securityController;
public UserController() {
}
public void login(ActionEvent event) {
try {
User userLogin = userEJB.fetchUserByEmail(user.getEmail());
} catch (Exception e) {
}
if (userLogin.getPassword().equals(user.getPassword())) {
securityController.setIsLoggedIn(true);
securityController.setIsAdmin(true);
securityController.setUser(user);
}
}
/**
* @return the user
*/
public User getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
答案 0 :(得分:1)
如果在代码中放入两个System.out.printlns怎么办?一个在异常处理程序中,一个在if语句之前。你得到了什么?
查看代码我会说你可以捕获异常,这应该是一个包含在EJB中的JPA异常。