users@glassfish.java.net

Does the DOA need to be an session bean as well?

From: <glassfish_at_javadesktop.org>
Date: Wed, 12 Aug 2009 03:38:52 PDT

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?

-------------------------------------------------------------------------------------------------
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");
            e.printStackTrace();
            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 {
            em.remove(cat);
        }
    }
 
    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 …>

<ejb-ref>
        <ejb-ref-name>CigCategoryLookup</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <remote>cig.bizlogicif.CategoryModelIF</remote>
        <ejb-link>CategoryModelImpl</ejb-link>
    </ejb-ref>
    </web-app>
[Message sent by forum member 'bess' (bess)]

http://forums.java.net/jive/thread.jspa?messageID=360064