I have a class with interface and a new class for which I call new ojbClass(params) - which contains an ejb call (code below). When it get's to call the ejb from that class, i get null point exception.
Class calling EJB method inside
public class ProfileDTO {
private Profile profileEntity;
@EJB
private ProfileRemoteBean profileBean; //remote bean is interface name
public ProfileDTO(String firstName, String lastName, Date birthDate, String gender, String country, String district, String city, String identificationNo, String idCardNo, String phone) {
createProfile();
}
public void createProfileEntity() {
Profile profileEntity = new Profile();
//deleted code for stackoverflow
//...................................
this.profileEntity = profileEntity;
}
public Profile getProfileEntity(){
return this.profileEntity;
}
private void createProfile() {
createProfileEntity();
profileBean.addProfile(getProfileEntity()); // profile bean null point here
}
}
EJB Interface
@Remote
public interface ProfileRemoteBean {
public List<Profile> getProfile();
}
EJB Class
@Stateless
public class ProfileBean implements ProfileRemoteBean,Serializable{
@PersistenceContext(unitName = "com.ulbs.admission.core_AdmissionCoreDBEJB_ejb_1.0-SNAPSHOTPU", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
@Override
public List<Profile> getProfile() {
TypedQuery<Profile> query = entityManager.createNamedQuery("Profile.findAll", Profile.class);
return query.getResultList();
}
}
Can you please give me some hints or a solution?
Thanks!
What you are trying is not supposed to work: You can't inject an EJB in a DTO because it is not a managed class.
Either refactor your dependency injection or pass the
ProfileRemoteBean
to theProfileDTO
via the constructor.