开发者

Why can't I catch EJB exceptions?

I can't seem to catch exceptions such as N开发者_C百科oResultException or EJBException in my java ee 6 project. Is there something I am doing wrong? I have caught exceptions outside the EJB container but this is my first time using EJB. Thanks.

@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;
    }


What if you put two System.out.printlns in the code? One in the exception handler and one right before the if statement. What do you get?

Looking at the code I would say you would be able to catch the exception, which should be a JPA exception wrapped in an EJB one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜