tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <>
Subject Re: Testing Services with access to Entities
Date Fri, 28 Mar 2008 19:42:25 GMT

On Mar 25, 2008, at 8:57 AM, <> <

 > wrote:
> Hi,
> I have want to test some internal used beans which access entity  
> beans.
> The problem is now, that the unit test wants to test a service which
> return an entity. This entity has a colelction member, something like
> this:
> @Entity
> Class A {
> Private List<B> bs;
> public List<B> getBs() {
> 	return bs;
> 	}
> ...
> }
> The collection bs is lazy initialized and should be. So, if I want to
> access the bs from the returned entity instance A, I get a
> LazyInitializationException. How can I test internal used services? Is
> there a godd way to do this? Until now I have only tested Beans which
> return transfer objects, but know this would be necessary.

Hi Karsten,

I'm not too sure what an org.hibernate.LazyInitializationException  
is.  I checked out the online api docs and it mentions that's what you  
get when a "session" is closed.  Not sure how a hibernate session  
relates to JPA concepts.

In your @PersistenceContext ref, are you using  
PersistenceContextType.EXTENDED or  
PersistenceContextType.TRANSACTION?  (TRANSACTION is the default if  

In your persistence.xml is your unit declared as transaction- 
type="RESOURCE_LOCAL" or transaction-type="TRANSACTION"?  (TRANSACTION  
is the default if unspecified)

Before I get the answers to those questions I'll make an uninformed  
guess that you're using TRANSACTION for both of the above and what's  
happening is your transaction is starting and completing around the  
ejb method that calls "getBs()" and returns the list.  By the time you  
test case (which isn't in a transaction) gets the List<B> the  
transaction has completed and all the refs in it are considered  
Detached in JPA terms.  That's just a guess, but if I'm right you  
could try switching to PersistenceContextType.EXTENDED and use an  
@Stateful bean, or you could run your unit test in a transaction which  
will widen the transactional scope from just the bean method to  
encompassing the while test method (


Let me know if my guess is right or wrong, the question is likely to  
come up again.


View raw message