10 Replies Latest reply on Nov 2, 2011 10:08 AM by Christian Sadilek

    Errai RPC calls with JPA's Extended Persistence Context?

    slawek t Novice

      I'm using Errai RPC fo server calls. On server-side there are RPC services which are CDI beans. Database operations are being managed by Hibernate.

       

      My question is how to use Extended Persistence Context in scenatio described below, because currently I have an issue like this:

       

      ...

      org.hibernate.PersistentObjectException: detached entity passed to persist: com.mycomp.proj.shared.domain.Group: org.hibernate.PersistentObjectException: detached entity passed to persist: com.mycomp.proj.shared.domain.Group

      ...

       

       

      The use case is creating a new user being assigned to groups - details in steps:


      1. On client side there is calling (by Errai RPC) remote service usersRPCService.saveNewUser(...)

      2. The service calls two additional CDI beans' methods - first for finding Group entities by theirs IDs, and second to save new User with assigned groups.

       

      Here a code snippets:

       

      UsersRPCService

       

      @Service

      @ApplicationScoped

      public class UsersRPCServiceImpl implements UsersRPCService {

       

       

        @Inject private UserTransaction utx;

                @Inject private UserService userService;

        @Inject private GroupService groupService;

       

       

          @Override

          public void saveNewUser( NewEditUserDTO newUserDto ) {

              User user = new User( );

              user.set... // here filling all user's properties on base of newUserDto content

              try {

                  utx.begin( );

                  List<Group> groups = groupService.getGroupsByIds( newUserDto.getGroupsIds( ) );

                  user.setGroups( groups );

                  userService.saveNewUser( user ); // here

      org.hibernate.PersistentObjectException: detached entity passed to persist

                  utx.commit( );

              }

              catch ( Exception e ) {}

          }

      }

       

       

      GroupService

       

      @ApplicationScoped

      @Transactional( TransactionPropagation.SUPPORTS )

      public class GroupService {

       

        @Inject Session session;

       

                public List<Group> getGroupsByIds( List<Long> ids ) {

              return session.createQuery( "select g from Group g where g.id in :ids" ).setParameterList( "ids", ids ).list( );

          }

      }

       

       

      UserService


      @ApplicationScoped

      @Transactional( TransactionPropagation.SUPPORTS )

      public class UserService {

       

       

          @Inject Session session;

       

          public User saveNewUser( User user ) throws Exception {

              session.save( user );

              session.flush( );

              return user;

          }

      }

       

      and Hibernate's Session producer

       

      @ApplicationScoped

      public class HibernateSessionProducer {

       

          @PersistenceContext( type = PersistenceContextType.EXTENDED )

          private EntityManager em;

       

          @Produces

          public Session getHibernateSession( ) {

              return ( Session ) em.getDelegate( );

          }

      }

       

      It looks like in groupService and userService there are used different sessions and group entities comming from groupService are just detached when assigning to user in usersRPCService.saveNewUser(...).

       

      I understand that it happens because of server-side Errai RPC calls implementation - beans are handled out of container's CDI context.

       

      I will be thankful for any advise about how to modify my code to have Extended Persistence Context active.