Question

I have created JPA entity USER. Then UserDao as interface, UserDaoBean as implementation.

@Stateless
public class UserDaoBean implements UserDao {
@PersistenceContext
private EntityManager em;
private CriteriaBuilder qb;

@Override
public User getUserByUsername(String username) {
    qb = em.getCriteriaBuilder();
    CriteriaQuery<User> c = qb.createQuery(User.class);
    Root<User> u = c.from(User.class);
    Predicate condition = qb.equal(u.get("username"), username);
    Logger.getLogger(UserDaoBean.class.getName()).log(Level.SEVERE, u.get("username").toString());
    c.where(condition);
    TypedQuery<User> q = em.createQuery(c);
    List<User> results = q.getResultList();
    if (results != null && !results.isEmpty()) {
        return results.get(0);
    }
    return null;
}

When I create eg. HomePage.java, corresponding HomePage.html with form, I can use

    @Inject
private UserDao userdao;

Everything works fine. But I created UsernameValidator:

public class UsernameValidator implements IValidator<String>{

    @Inject
    private UserDao userDao;   

    @Override
    public void validate(IValidatable<String> iv) {
        String username = iv.getValue(); 
            User user = userDao.getUserByUsername(username);
...
        }       

}

There is NullPointerException on userDao. I don't understand, how come the injecion works in the page, but not in the Validator.

Was it helpful?

Solution

You UserNameValidator class is an unmanaged one, and not one of the classes that Wicket automatically injects (not a Behavior / Component).

Thus you need to call CdiContainer.get().getNonContextualManager().inject(this); into your class constructor to trigger CDI injection and use injected beans.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top