0

I've just started to work with EJB. I want to recieve some data from database using JTA. My entity:

@Entity
@Table(name = "User")
@NamedQuery(name = "User.findByAddressName",
            query = "select u from User u where u.userAddress.addressName = :addressName")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long userID;

     @Column(name = "userFirstName", length = 45)
     private String userFirstName;

    @Column(name = "userLastName", length = 45)
    private String userLastName;

    @Column(name = "userBirthDate")
    private Date userBirthDate;

    @Column(name = "userPhone")
    private String userPhone;

    @Column(name = "userPassword")
    private String userPassword;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "addressUser")
    private Email userAddress;

    public User() {
    }

    public User(String userFirstName, String userLastName, Date userBirthDate, String userPhone, String userPassword) {
        this.userFirstName = userFirstName;
        this.userLastName = userLastName;
        this.userBirthDate = userBirthDate;
        this.userPhone = userPhone;
        this.userPassword = userPassword;
    }
}

My DAO:

@Stateless
public class UserBean {

    @PersistenceContext(unitName = "server")
    private EntityManager em;

   public User getUserByEmail(String email) {
       TypedQuery<User> queryUser = em.createNamedQuery("User.findByAddressName",
               User.class);
       queryUser.setParameter("addressName", email);
       return queryUser.getSingleResult();
   }

}

My service:

@Stateless
public class LoginService {
    @EJB
    UserBean userBean;

    public boolean loginUser(String userEmail, String userPass) {
        User user = userBean.getUserByEmail(userEmail);

        if (userPass.equals(user.getUserPassword())) {
            return true;
        } else return false;
    }
}

I try to recieve data:

boolean b = new LoginService().loginUser("user", "user1");

But UserBean userBean, injected by @EJB in LoginService is null.

There are a lot of simple examples of how to do such things, I do all exactly the same, but it's still throwing NullPointerException, I do't know why.

Mary Ryllo
  • 2,321
  • 7
  • 34
  • 53

1 Answers1

1

To benefit from injection within Java EE environment you can't instantiate your service via new. You need to either inject it via @EJB or lookup it using JNDI.

If you create your service via new this instance isn't considered as container-managed instance so that none of its features (like injection) are available. When you inject it or lookup its reference via JNDI yo get fully managed instance with all injections done for you.

Don't know on which Java EE stack you are on (5 or 6) but keep in mind that you cannot simply inject things via @EJB into classes that were created by hand. They need to be also created by container to make use of injection.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Michal Ostruszka
  • 2,089
  • 2
  • 20
  • 23