Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??

---------------------------EJB---------------------------------------------------------------------------------------

@Stateless
@Remote
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();
}

----------DAO---------------------------------------------------------------------------------
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>
reply
    Bookmark Topic Watch Topic
  • New Topic