Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
nikola petanjak
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • 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
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are in a filter you could call from the filter like this:
 
Valery Lezhebokov
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Wow, my bad. edited up there.

You can do like this:
 
nikola petanjak
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • 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: 21
1
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!