wood burning stoves 2.0*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
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: 114
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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: 114
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://aspose.com/file-tools
 
subject: Access to entity manager in Session Bean
 
Similar Threads
J2EE Design Question -- Am I On The Right Track?
J2EE, encapsulation & data with operations
Changes in Entity Bean are not saved
Doubts in CMT vs BMT beans
Cleared SCBCD last week.. some notes