users@glassfish.java.net

Re: EJB3 NameNotFoundException with simple local sample

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 20 Sep 2007 10:24:07 -0700

Check slides 23-38 at this JavaOne2007 session [1]. They might be helpful.

thanks,
-marina

[1] http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-4593.pdf

angelogeminiani wrote:
> Thank you Sahoo, I did it.
> I've read the post, and I think I did every thing like in sample.
> I have an Entity pojo, call it NewsEntity, and a simple EJB
> (NewsEntityFacade).
> Here is code of my simple bean:
>
> @Stateless
> public class NewsEntityFacade implements INewsEntityFacadeLocal {
>
> @PersistenceContext (unitName="Test_Sample1-ejbPU")
> private EntityManager em;
>
> /** Creates a new instance of NewsEntityFacade */
> public NewsEntityFacade() {}
>
> public void create(NewsEntity newsEntity) {
> em.persist(newsEntity);
> }
>
> public void edit(NewsEntity newsEntity) {
> em.merge(newsEntity);
> }
>
> public void destroy(NewsEntity newsEntity) {
> em.merge(newsEntity);
> em.remove(newsEntity);
> }
>
> public NewsEntity find(Object pk) {
> return (NewsEntity) em.find(NewsEntity.class, pk);
> }
>
> public List findAll() {
> return em.createQuery("select object(o) from NewsEntity as
> o").getResultList();
> }
>
> }
>
> I have only one persistence unit, called "Test_Sample1-ejbPU" (I could omit
> unitName).
> My sample works fine if I declare @EJB in a test servlet, or web service.
> However, I'm not able to use JNDI lookup, nor injection, inside a simple
> class.
>
> I cannot understand what is wrong in my code.
> I read JavaEETutorial from Sun site, but didn't find an answer.
> Why is not possible use lookup in a simple object?
>
> I'm a little confused...
>
> I'm using code from this NetBeans example:
> http://www.netbeans.org/kb/55/ejb30.html
> http://www.netbeans.org/kb/55/ejb30.html
>
> At NetBeans tutorial, I just added a new class, SimplePOJO, in wich I added
> some lookup tests (all failed).
> Here is my entire SimplePOJO class.
>
> public class SimplePOJO {
>
> /** Creates a new instance of SimplePOJO */
> public SimplePOJO() {
> this.lookup();
> }
>
> public void test(){
> this.lookup();
> }
>
> private void lookup(){
> try {
> Context c = new InitialContext();
> Object obj = null;
> String name= null;
>
> try{
> name = NewsEntityFacade.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/" +
> NewsEntityFacade.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/ejb/" +
> NewsEntityFacade.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/NewsEntityFacade";
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/ejb/NewsEntityFacade";
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
>
> try{
> name = INewsEntityFacadeLocal.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/" +
> INewsEntityFacadeLocal.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/ejb/" +
> INewsEntityFacadeLocal.class.getName().toString();
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/INewsEntityFacadeLocal";
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
> try{
> name = "java:comp/env/ejb/INewsEntityFacadeLocal";
> obj = c.lookup( name );
> System.out.println("*** OK: " + name);
> } catch (NamingException ex){
> System.out.println("*** FAILED: " + name);
> }
>
>
> } catch (NamingException ex) {
> ex.printStackTrace();
> }
> }
>
> }
>
> Is there something wrong in it?
>
>
>
> Sahoo wrote:
>
>>I think you should take a look at a web->ejb->jpa example first. Take a
>>look at a simple example [1] that illustrates the main points.
>>
>>-- Sahoo
>>
>>[1]
>>http://weblogs.java.net/blog/ss141213/archive/2005/12/using_java_pers.html
>>angelogeminiani wrote:
>>
>>>Hi Sahoo,
>>>I tried add ejb-jar.xml file to my EJB Module.
>>>But still doesn't work.
>>>I put ejb-jar.xml file into WEB-INF, is it right?
>>>
>>>I need to look up the SimpleBean from the SimplePOJO because my
>>>SimplePOJO
>>>contains an entity manager:
>>>
>>> @PersistenceContext (unitName="Test_Sample1-ejbPU")
>>> private EntityManager em;
>>>
>>>Is there another way to create SimpleBean?
>>>If I do:
>>>SimpleBean bean = new SimpleBean();
>>>The EntityManager (em) has not been injected and is null.
>>>
>>>Angelo.
>>>
>>>
>>>Sahoo wrote:
>>>
>>>
>>>>When the SimplePOJO is packaged in the same jar along with SimpleBean, I
>>>>don't know why you want to look up the SimpleBean from the SimplePOJO.
>>>>But any way, yes, you need a ejb-jar.xml like this to do a EJB look up
>>>>from a POJO. Here is an example: (This works as long as SimplePOJO is
>>>>called in the context of SimpleBean, for the ejb-loca-ref is defined in
>>>>the env context of SimpleBean only.
>>>>
>>>><ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee">
>>>><enterprise-beans>
>>>> <session>
>>>> <ejbname>SimpleBean</ejbname>
>>>> <ejb-local-ref>
>>>> <ejb-ref-name>ejb/SimpleBean</ejb-ref-name>
>>>> <ejb-ref-type>Session</ejb-ref-type>
>>>> <local>packagename.SimpleLocal</local>
>>>> <ejb-link>SimpleBean</ejb-link>
>>>> </ejb-local-ref>
>>>> </session>
>>>></enterprise-beans>
>>>><ejb-jar>
>>>>
>>>>Thanks,
>>>>Sahoo
>>>>
>>>>angelogeminiani wrote:
>>>>
>>>>
>>>>>Hi,
>>>>>I'm newbie in EJB3 and I've been struggling for 2 days trying to do a
>>>>>simple
>>>>>JNDI lookup.
>>>>>I have a stateless session bean (EJB 3.0) in an EJB Module project, and
>>>>>a
>>>>>Web Project.
>>>>>The two projects are packaged in a Enterprise Application project.
>>>>>
>>>>>In my EJB Module I have a POJO that need lookup to ebj object
>>>>>(SimpleBean).
>>>>>SimpleBean and SimplePOJO are in same EJB Module.
>>>>>
>>>>>The EJB:
>>>>>
>>>>>@Local
>>>>>public interface SimpleLocal{
>>>>>...
>>>>>}
>>>>>
>>>>>@Stateless()
>>>>>public class SimpleBean
>>>>> implements SimpleLocal{
>>>>>...
>>>>>}
>>>>>
>>>>>The POJO:
>>>>>
>>>>>public class SimplePOJO {
>>>>>
>>>>> // .ctor
>>>>> public SimplePOJO (){
>>>>> Context c = new InitialContext();
>>>>> SimpleLocal myBean =
>>>>>(SimpleBean)c.lookup("java:comp/env/ejb/SimpleBean");
>>>>> }
>>>>>
>>>>>}
>>>>>
>>>>>I tried use my SimpleBean from a servlet using annotation @EJB and here
>>>>>works fine.
>>>>>But in my SimplePOJO constructor I get a
>>>>>javax.naming.NameNotFoundException.
>>>>>
>>>>>Is there something wrong in my code?
>>>>>Do I need a .xml descriptor?
>>>>>
>>>>>Thank you,
>>>>>Angelo
>>>>>
>>>>>
>>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>>>For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>>
>>
>
>