-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Entity bean as parameter to SLSB method
PostPosted: Tue Dec 11, 2007 1:39 pm 
Newbie

Joined: Tue Oct 02, 2007 2:42 am
Posts: 2
JBoss 4.2.2
MySQL 5.0

Is it possible to use an entity bean as a parameter to a stateless session bean method? I would guess so, but...

I have one SLSB something like this:

Code:
@PersistenceContext(unitName="myunit") EntityManager em;

@EJB OtherSLSB slsb2;

...

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void methodX(Integer id) {
  FooEntity foo = findFooEntity(id);
  ...
  slsb2.methodY(foo);
}


and another SLSB:

Code:
@PersistenceContext(unitName="myunit") EntityManager em;

...

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void methodY(FooEntity foo) {
  // Try to do something with foo here
}


Whatever I try, I can't do anything much with foo in methodY. I either get LazyInitializationExceptions, or if I merge the foo entity, I get other exceptions related to stale states etc. Isn't this supposed to work? Shouldn't the transaction in the first SLSB propagate to the second one, not causing any lazy exceptions?! Am I missing something obvious?


Top
 Profile  
 
 Post subject: Re: Entity bean as parameter to SLSB method
PostPosted: Tue Dec 11, 2007 6:27 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
MagnusNord wrote:
Whatever I try, I can't do anything much with foo in methodY. I either get LazyInitializationExceptions, or if I merge the foo entity, I get other exceptions related to stale states etc. Isn't this supposed to work? Shouldn't the transaction in the first SLSB propagate to the second one, not causing any lazy exceptions?! Am I missing something obvious?



This might not exactly answer your question but my guess is that entity managers are not the same. I do believe in hibernate you have a way to say a session-and probably therefore an entity manager-is bound to a transaction scope which means you will get the same session so you won't have these problems. however, if this is unavailable in the JPA world (or hibernate JPA world) you will have to initialize all lazy properties manually. You will have the same nature of problem if you cache this entity.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 4:23 am 
Newbie

Joined: Tue Oct 02, 2007 2:42 am
Posts: 2
Giving it a good night's sleep, I found the problem. The findFooEntity method was not annotated correctly.

I have annotated my SLSBs with NOT_SUPPORTED as the default transaction attribute (i.e. annotated the class). However, this makes the system very error prone, as forgetting to annotate single methods generate exceptions when lazy loading etc. Changing the default to SUPPORTS solved my problem (or, of course, annotating the findFooEntity method).

Is there any best practice regarding this? Any pros and cons?

Any tips on how to debug scenarios like this? I think it's a bit tricky to figure out where it goes wrong :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.