On Mar 25, 2008, at 8:57 AM, <[EMAIL PROTECTED]> <[EMAIL PROTECTED] > 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 unspecified)

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 (http://openejb.apache.org/3.0/unit-testing-transactions.html ).

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

-David

Reply via email to