• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access to entity manager in Session Bean

 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic