| 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
|
|
|
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...".
|
 |
 |
|
|
subject: Access to entity manager in Session Bean
|
|
|