EJB life cycle method

The various stages through which an enterprise bean go through its lifetime is known as the life cycle of EJB.

EJB life cycle method

EJB life cycle method

     

The various stages through which an enterprise bean go through its lifetime is known as the life cycle of EJB. Each type of enterprise bean has different life cycle. Here we are telling you about the lifecycle of message driven bean. This type of bean follow three steps:

1)setMessageDrivenContext:-This method is used to pass the context object to the instance.

2)ejbCreate:-
This method is generated automatically whenever a new enterprise bean is created.

3)ejbRemove:-
At this stage the bean instance is ready to move for the garbage collection.

em.persist(newsEntity):-This method makes an entity instance that is managed and persistence.

em.merge(newsEntity):-By using this method we can merge the state of the given entity into the current persistence context.

em.remove(em.merge(newsEntity)):-This method is used for removing the entity instance.

Here is the program denoting life cycle of message driven bean.

package ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class NewsEntityFacade implements NewsEntityFacadeLocal {
@PersistenceContext
private EntityManager em;

public void create(NewsEntity newsEntity) {
em.persist(newsEntity);
}

public void edit(NewsEntity newsEntity) {
em.merge(newsEntity);
}

public void remove(NewsEntity newsEntity) {
em.remove(em.merge(newsEntity));
}
}

Download source code