Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home C-tutorials EJB life cycle method

Related Tutorials


 
 

Share on Google+Share on Google+

EJB life cycle method

Advertisement
This page discusses - 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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: EJB life cycle method  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com