This question has been asked once here EJB 3 Session Bean Design for Simple CRUD, and I just want to ask more in depth questions about this design. I already tried to ask the questions at the original post, however, I did not see any respond, so I decide to create new post. So Pascal solution of implement generic CRUD session bean is as follow
public interface GenericCrudService {
public <T> T create(T t);
public <T> T find(Class<T> type, Object id);
public <T> void delete(T t);
public <T> T update(T t);
public List findWithNamedQuery(String queryName);
public List findWithNamedQuery(String queryName, int resultLimit);
public List findWithNamedQuery(String namedQueryName,
Map<String, Object> parameters);
public List findWithNamedQuery(String namedQueryName,
Map<String, Object> parameters,
int resultLimit);
public <T> List<T> findWithNativeQuery(String sql, Class<T> type);
}
AND
@Stateless
@Remote(GenericCrudService.class)
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class GenericCrudServiceBean implements GenericCrudService {
@PersistenceContext
private EntityManager em;
@Override
public <T> T create(T t) {
em.persist(t);
return t;
}
@Override
public <T> T find(Class<T> type, Object id) {
return em.find(type, id);
}
@Override
public <T> void delete(T t) {
t = em.merge(t);
em.remove(t);
}
@Override
public <T> T update(T t) {
return em.merge(t);
}
@Override
public List findWithNamedQuery(String queryName) {
return em.createNamedQuery(queryName).getResultList();
}
@Override
public List findWithNamedQuery(String queryName, int resultLimit) {
return em.createNamedQuery(queryName).setMaxResults(resultLimit)
.getResultList();
}
@Override
public List findWithNamedQuery(String namedQueryName,
Map<String, Object> parameters) {
return findWithNamedQuery(namedQueryName, parameters, 0);
}
@Override
public List findWithNamedQuery(String namedQueryName,
Map<String, Object> parameters,
int resultLimit) {
Query query = this.em.createNamedQuery(namedQueryName);
if(resultLimit > 0) {
query.setMaxResults(resultLimit);
}
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
return query.getResultList();
}
@Override
@SuppressWarnings("unchecked")
public <T> List<T> findWithNativeQuery(String sql, Class<T> type) {
return em.createNativeQuery(sql, type).getResultList();
}
}
Questions:
1) When I try to reference this EJB bean in my managed bean, I have to do
@EJB
private GenericCrudService myEJB;
instead of
@EJB
private GenericCrudServiceBean myEJB;
This make little sense to me since GenericCrudService
is just an interface, GenericCrudServiceBean
is the Stateless Bean
I get this exception when I do GenericCrudServiceBean
,
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.bridgeye.web.Profile/myEJB' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.bridgeye.web.Profile/myEJB,Remote 3.x interface =com.bridgeye.ejb.GenericCRUDServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.bridgeye.ejb.GenericCRUDServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'com.bridgeye.ejb.GenericCRUDServiceBean#com.bridgeye.ejb.GenericCRUDServiceBean' [Root exception is javax.naming.NamingException: Lookup failed for 'com.bridgeye.ejb.GenericCRUDServiceBean#com.bridgeye.ejb.GenericCRUDServiceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: com.bridgeye.ejb.GenericCRUDServiceBean#com.bridgeye.ejb.GenericCRUDServiceBean not found]]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:597)
2) what is the different between javax.rmi.Remote and javax.ejb.Remote? If I took out annotation Remote, does my EJB become local or I have to specify with annotation Local
3) In my managed bean, I do a simple myEJB.find(User.class, id)
, but if I have @TransactionAttribute(TransactionAttributeType.MANDATORY)
, then I run into exception, it work fine if I took out TransactionAttribute
statement. Any idea why?