0

I want to create a remote EJB for an entity class. Is there a way of implementing a method with one parameter beign an object of a class that specifically has the @Entity annotation? The purpose of this, is to create just one bean for all my entities.

Example:

public void save(Entity ent){
     em.persist(ent);
}

1 Answer 1

1

If you just have 1 ejb for all entities you will soon run into problems when you have to handle some of them in a special way.

But you can do like this with an abstract super-ejb, and still have 1 ejb per entity, but its very easy to create. And you can still overwrite the default, inherited methods.

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

    public AbstractEjb(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);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

}

To implement for an entity just do

@Stateless
public class TestEjb extends AbstractEjb<TestEntity> {

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

    public TestEjb() {
        super(TestEntity.class);
    }

    public EntityManager getEntityManager() {
        return em;
    }
}

Source: Generate JSF pages from entity classes in Netbeans.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.