Question

Actually, I'm migrating an application from javaee6 to javaee7 which has ejb module with some CDI components and all business logic.

In general, I had an interface to make CRUD with entities:

public interface StoreManager<T> {
    T create(T entity) throws SystemException;
}

And an implementation like this:

public class StoreManagerImpl extends AbstractManager<Object> implements StoreManager<Object> {

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

    private void setEntityManager(EntityManager em) {
        this.em = em;
    }

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

    @Produces @Generator
    public StoreManager create() {
        StoreManagerImpl smImpl = new StoreManagerImpl();
        smImpl.setEntityManager(em);
        return smImpl;
    }

}

The abstract class AbstractManager is where are defined all methods like create, read, update,etc.

public abstract class AbstractManager<T> {

    public AbstractManager() {
    }

    protected abstract EntityManager getEntityManager();

    public <P> P crear(P entity, Class<?> clase) throws SystemException {
        try {
            getEntityManager().persist(entity);
            getEntityManager().flush();
            getEntityManager().refresh(entity);
        } catch (EntityExistsException ex) {
            throw new EntityExistsException("El registro ya existe en la base de datos.");
        } catch (EJBException ex) {
            throw new SystemException("El registro no pudo ser guardado.");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new SystemException("El registro no pudo ser guardado.");
        }
        return entity;
    }
}

My @Generator is a Java annotation that will provide a way for services to call operations passing an entity through generic to field level. That is:

@Inject
@Generator
private StoreManager<Person> storeManager;
...
..
storeManager.create(person);
....

With glassfish 3 and javaee7 everything is working, but javaee7 seems to have some specifications that doesn't make possible my implementation. Does anyone know about this problem or could help me to solve?

Thanks a lot and sorry for my poor english.........

Was it helpful?

Solution

You're correct, there is a change in the spec that makes your original code not work. I believe the issue is under review in CDI 1.2 to be reverted. Basically, the way CDI 1.1 is written you have to have a concrete implementation of StoreManager<Person> (e.g. PersonStoreManager extends StoreManagerImpl<Person>).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top