Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Is it possible to call session bean from plain java class within same project?

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.

Please excuse this beginner's question but what bothers me last few days is:

I have ejb project. In the same package i have plain Java class as well as session bean, which uses JPA entityManager to persist data.
Now my question is, is it possible to call that session bean inside of "plain" Java class? I know i can't use annotation. Trying to get bean with context lookup method has gave me headacke only so far...Please any suggestions would be appreciated.

Thank you all in advance.

Nikola
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are in a filter you could call from the filter like this:
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are outside of the container (or create an object yourself) then the only way is the JNDI lookup. In EJB 3.x it shouldn't be that painful since the JNDI names have been standardized under java:global namespace.
I suppose the method mentioned by Hebert Coelho would work for JSF ManagedBeans but not for EJBs.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, my bad. edited up there.

You can do like this:
 
nikola petanjak
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for fast replies. I have some questions tough.

How do i know my bean's JNDI name?
Does it matter that i use WebSphere application server?
Do i have to call bean by remote interface?

I have no problem in calling this bean from managed bean in JSF project, it has defined ej-local-ref in web.xml.
In a lookup i use java:comp/env
Do i need to define something similar in ejb-jar? Because i have no problem at current configuration to inject beans in one another.

If i try global lookup i get error
javax.naming.NameNotFoundException: Name global not found in context "java:"


 
nikola petanjak
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is solved!

In ejb-jar.xml i added this (i read that in EJB3.0 this file is optional). I guess i needed to explicitly say information about this bean because i can't use annotation in
plain java class.



In my plain java class i search context following way:



Once again, thank you very much for suggestions.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic