Suggested Pages

Wednesday, June 13, 2012

Remote Stateless Session Beans, Entity Beans and Serialization

Let's suppose you have an enterprise method of a Remote Stateless Session EJB like: public void save(Contact contact). Contact is an entity beans that must be a Serializable class.
Contact class needs to be serializable because a Remote Stateless Session EJB uses RMI protocol to communicate over network and RMI uses serialization to pass objects between JVMs.
If you don't make Contact a Serializable class, an exception may occur: java.io.NotSerializableException, when you invoke the enterprise method.
ServiceContactEJBRemote.java
@Remote
public interface ServiceContactEJBRemote {

 public void save (Contact contact);
 
}
ServiceContactEJB.java
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ServiceContactEJB implements ServiceContactEJBRemote {

 @PersistenceContext(unitName = "serviceContact")
 private EntityManager entityManager;

 @TransactionAttribute(TransactionAttributeType.REQUIRED)
 public void save(Contact contact) {
  entityManager.persist(contact);
 }

Contact.java

@Entity
@Table
public class Contact implements Serializable{

 @Id
 private int id;
 
 @Column
 private String number;
 
 @Column
 private String type;

        ...

No comments :

Post a Comment

Suggested Pages