Can I get something like an entity bean in EJB 3.2?

1.2k views Asked by At

I was reading a Java EE book recently, and apparently entity beans were recently removed from the EJB specification. You are supposed to use JPA instead. But I want entity beans!! What I am really looking for is a JPA persistent entity that is remotely accessible, like an EJB. Something like this:

@Entity
@Remote(MyEntityRemote.class)
@LocalBean
public class MyEntityEJB implements MyEntityRemote {
    public void doSomething() {
        // actually do something
    }
}

Is this at all possible without removing the bean annotations and writing a session bean like this:

@Stateless
@Remote(StatelessInterfaceToMyEntityRemote.class)
@LocalBean
public class StatelessInterfaceToMyEntity implements StatelessInterfaceToMyEntityRemote {
    public void doSomething(MyEntity entity) {
        entity.doSomething();
    }
}
1

There are 1 answers

1
CodeSamurai777 On

If I understand you correctly it is possible first you create an Entity:

@Entity
@Table('MyEntityTable')
public class MyEntity {...}

Then you create a session bean facade for the entity exposing through it any interfaces you may need

@Stateless //Facade is a seesion bean so it can be @Stateless or @Statefull for basic CRUD it shoud be @Stateless
public class EntityFacade extends AbstractFacade<MyEntity> {
@PersistenceContext(unitName = "MyPersistanceUnit") //remember to define it first
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

Now u can define any methods that work with your Entity class. Remember if you expose your entity via Remote Interfaces it will be in a detached state. So after updating you will first nee to use the merge(object) method of EntityManager

EDIT
Abstract facade is a concept that is used with JPA Entity, NetBeans in version 7.3 generates it for you automatically. It is used to define the most common operations on Entities so you dont have to repeat the code for every Entity. It goes like this

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
    getEntityManager().persist(entity);
}

public void edit(T entity) {
    getEntityManager().merge(entity);
}

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
}

public T find(Object id) {

    return getEntityManager().find(entityClass, id);
}
...
}

The function above do some basic CRUD operations without much effort. So extending the facade gives you the ability to have does operations defined you could say out of the box. Of course this just for basic configurations the Entity facade can use many entities and do some business logic before it persists anything. So in your case it would go like this:

public class EntityFacade extends AbstractFacade<MyEntity> {
@PersistenceContext(unitName = "MyPersistanceUnit") //remember to define it first
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
 public doSomething(MyEntity entity)
{
 entity.get(...);
  ...
 entity.set(...)
  if(iWantToPesristIt)
    edit(entity)
  else
    return;
}
}