Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rephrase: Does the DOA need to be an session bean as well? SOS.... lossing hairs...

Bess Moore
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I been working in this problem for days, I am a bit confuse, I have created a session bean which contains biz logic and EAO layer,
I understand that we can get the persistence with @PersistenceContext private EntityManager em;

Correct me if I am wrong I am a bit sceptical to use the em in the session bean. (there been lot of arguments stated that we need to ditch )

BUT, still I would want use EntityManager in the EAO layer due to just in case be cause of the mobility of the DAO layer in the future.

So I have implemented DAO design pattern at below but is not working, is giving me:

Unable to get an Entity Manager Instance
javax.naming.NameNotFoundException: No object bound to name java:comp/env/persistence/em

I am so confuse, does the DAO need to be a session bean as well? otherwise, what have it miss??


public class CategoryModelImpl implements CategoryModelIF {

public CategoryModelImpl() { }

public Category[] getAllCategorys() throws CigCategoryException {
EAOFactoryManager eaoFactoryMgr = EAOFactoryManager.getEAOFactoryManager(EAOFactoryManager.TOPLINK);
CategoryEAOIF catDAO = eaoFactoryMgr.getCategoryEAOIF();
return catDAO.getAllCategorys();

import exception.CategoryException;
import bizlogicif.CategoryModelIF;
import java.util.*;
import javax.ejb.*;
import javax.persistence.*;

import persistence.entities.Category;
import javax.naming.*;

public class CategoryEAOImpl implements CategoryEAOIF{

private EntityManager em;

public CategoryEAOImpl(){
em = getEntityManager();

private EntityManager getEntityManager() {
try {
InitialContext ctx = new InitialContext();
return (EntityManager) ctx.lookup("java:comp/env/CigCategoryLookup");
} catch (Exception e) {
System.out.println("Unable to get an Entity Manager Instance");
return null;

public void deleteCategory(Category cat)throws CategoryException{
String id = cat.getCatId();
cat = em.find(Category.class, id);
if(cat == null) {
throw new CategoryException("Record for " + id + " not found");
} else {

public Category[] getAllCategorys()throws CategoryException{
Query query = em.createNativeQuery("SELECT cat_id, cat_name, cat_description, version FROM Category", Category.class);
List categories = query.getResultList();
return (Category[])categories.toArray(new Category[0]);

In web xml
<web-app …>

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic