File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Access to entity manager in Session Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "Access to entity manager in Session Bean" Watch "Access to entity manager in Session Bean" New topic
Author

Access to entity manager in Session Bean

Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 113
hi,

I can access entity manager in my bean class without problem, but when I invoke method from another class, packed inside same jar archive, entity manager is always null. Example: I have session bean named ItemPurchaseBean. Inside one of it's methods I call getCustomerBalance() from helper class Customer (in subpackage). I have put it out of ItemPurchaseBean to make my code cleaner. Perhaps it is bad habit, but I really can't see better solution than this.

So the question is - how to get entity manager inside that class? I tried passing as argument from ItemPurchaseBean, but also was null.

Or can't I use entity manager outside bean class? And then only via @Resource annotation I can only access datasource?

Michal.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Well if that Customer helper class is also a session bean, then instead of instantiating it, you should inject it into ItemPurchaseBean using the EJB annotation. The container doesn't inject dependencies when you directly instantiate any session bean...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 113
Ankit Garg wrote:Well if that Customer helper class is also a session bean, then instead of instantiating it, you should inject it into ItemPurchaseBean using the EJB annotation. The container doesn't inject dependencies when you directly instantiate any session bean...


actually it meant to be POJO. I really still not convinced creating many ejb after 2.1 standard. What are the common practices in such cases then? Perhaps anyone has example of more complex ejb code, not like in tutorial where you create just one method...

BTW. I changed persisentce transaction type from JTA to ResourceLocal, but that results in exception "unable to commit inside container managed transaction...".
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Access to entity manager in Session Bean
 
Similar Threads
Cleared SCBCD last week.. some notes
J2EE Design Question -- Am I On The Right Track?
Doubts in CMT vs BMT beans
Changes in Entity Bean are not saved
J2EE, encapsulation & data with operations