On Jun 2, 2012, at 3:43 PM, georgenis wrote:
> Hi,
>
> here is a short preview of my method:
>
> @Override
> public RoleInformation getRole(String roleId, String sessionId) throws
> OpenflowException {
> Session session = sessionBean.isSessionValid(sessionId);
> if(session!=null) {
> RoleInformation roleInformation = (RoleInformation)
> entityManager.find(RoleInformation.class, roleId);
> if(roleInformation==null)
> throw new OpenflowException("Role does not
> exists >>> "+roleId);
> return roleInformation;
> }
> return null;
> }
>
> The entityManager will be injected by
> @PersistenceContext(unitName="myUnit").
Is the class that declares `getRole` an EJB? If not then there will be no
transaction in progress and the `entityManager.find(RoleInformation)` is going
to return a detached `RoleInformation` instance. So any data lazily loaded
will not be loadable inside the `getRole()` method (or outside that method when
`RoleInformation` is turned into xml).
This is really a lazy loading issue. The options are to either not use lazy
loading or to "touch" the lazily loaded fields while the entity is still
attached to the entityManager. Probably the second one is preferable.
To do that you need to start a transaction before you execute
`entityManager.find(RoleInformation.class..)`, the iterate over the collections
that are lazily loaded, then commit the transaction.
To create a transaction around your `entityManager.find()` call you either need
to:
- add `@Resource UserTransaction userTransaction;` to your Web Service class
then use the `userTransaction` to begin/commit
- Annotate your web service class with `@Stateless` or `@Singleton`, then
you'll get the transaction for free. If you chose `@Singleton` make sure you
annotate the `getRole()` method with `@Lock(READ)` so that method can be
multithreaded.
-David