0
votes

Could anyone help me? There is a SessionScoped Managed Bean and one Stateless Ejb and one other Stateful Ejb..

The serachCustomer() method in the MB call the injected DaoEjbTst searcCustomer() method which return with an instance of BCus entity object. I injected in to Stateless DaoEjbTst Ejb the other Stateful CustomerSession EJB and when the entity instance is ready in the DaoEjbTst EJB i call the CustomerSession EJB setActualCustomer method and i give as paramter the entity instance for this method and try to store it... Then when i try to get this "stored" entity instance with another showTstDate() method in the ManagedBean it's throws NullPointer Exception.. And i dont know why.. Why doesn't exist the public BCus actualCustomer paramter in the stateful ejb? I tryed to create @PreDestroy and @PrePassivate and @Remove methods in the Stateful Ejb to check if the container remove it but this methods never invoked by the container.. So i'm sure the ejb exist.. but inspite of this i can't access it :( I don't use interfaces.

Here is my managedbean:

@EJB
private DaoEjbTst daoEjb;

@EJB
private CustomerSession customerSession;

    public void serachCustomer() throws IOException {
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (daoEjb.searcCustomer(custNo)) {

            ctx.getExternalContext().redirect("showCustomer.xhtml");

        }
        else {
             ctx.getExternalContext().redirect("test.xhtml");
        }

    }

    public String showTstDate() {
        log.info("MB EJB EXIST: " + customerSession);
        return "Test: " + customerSession.getActualCustomer().getCustName();

    }

Here is my DaoEjbTst:

@Stateless
public class DaoEjbTst {

    private final Logger log = Logger.getLogger("DaoEjbTst.class");

    @EJB
    private CustomerSession customerSession;

    public CustomerSession getCustomerSession() {
        return customerSession;
    }

    public void setCustomerSession(CustomerSession customerSession) {
        this.customerSession = customerSession;
    }


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


    public boolean searcCustomer(String custNo) {


        try {
        BCus cus = (BCus) em.createNamedQuery("BCus.findByCustomerno").setParameter("customerno", custNo).getSingleResult();
            log.info("DAOEJB: " + cus);
        customerSession.setActualCustomer(cus);
        return true;
        }
        catch (NoResultException e) {
            log.info(e.getMessage());
        return false;
        }


    }

And here is my CustomerSession EJb:

@Stateful
public class CustomerSession {

    public BCus actualCustomer;

    private final Logger log = Logger.getLogger("CustomerSession.class");

    public BCus getActualCustomer() {
        return actualCustomer;
    }

    public void setActualCustomer(BCus actualCustomer) {
        this.actualCustomer = actualCustomer;
        checkTst();
    }

    public CustomerSession() {
    }



}
1
now i tried to create a local interface for the stateful ejb.. but i still can't access is.. still throws null pointer.solarenqu

1 Answers

0
votes

I think that CustomerSession bean injected in ManagedBean isn't the same bean instance that is injected in DaoEjbTst. So invoking:

customerSession.getActualCustomer()

in ManagedBean simply returns null because ActualCustomer field was not set for this particular bean instance. It was set in DaoEjbTest but this is different instance of CustomerSession. So:

DaoEjbTst.getCustomerSession().equals(ManagedBean.getCustomerSession())

gives false.

When you look into specification EJB 3.1 (section 3.4.7.1) you see:

A stateful session object has a unique identity that is assigned by the container at the time the object is created.

Basically what you should do is to setActualCustomer for the instance of CustomerSession bean in ManagedBean with value that is found in DAO's searcCustomer() method. However storing stateful session bean inside stateless bean is a very bad idea and I suggest you to rethink your architecture.