3 Replies Latest reply on Aug 3, 2005 6:24 PM by Emmanuel Bernard

    Reassigned existing entities to other entities does not work

    Alexander Hartner Newbie

      I have the following scenario. A User has several Roles. The relationship between user and role is defined as a bi-directional many-to-many relationship.

      - User A has Role A.

      - To retrieve Roles use :
      User userA = ....
      Roles roles = userA.getRoles()

      - Then I contruct a new user and assign roles to it
      User userB = new User();

      - Then I pass my use to the UserManagerBean to persist it

      -At this point what I percieve is happening is that all the roles which were previously associated with userA are now reassigned to userB. I have tried retrieving the "new" roles which resulted in :

      User userB = new User();

      - In this case the behaviour was correct :-)

      The relationship between user and roles is a ManyToMany relationship without cascading and I am useing JBoss 4.0.3 RC 1 on JDK 1.5.0_04 on Windows XP.

      Any suggestions on this. I believe this to be a bug.

        • 1. Re: Reassigned existing entities to other entities does not
          Emmanuel Bernard Master

          don't copy the collection instance, add the entities to a new collection isntance.

          • 2. Re: Reassigned existing entities to other entities does not
            Alexander Hartner Newbie

            Basically the solution is to manually ensure that the relations are within the entity context / session. This is done my attempting a refresh, which might fail with an exception if the bean has not yet been created.

            org.hibernate.UnresolvableObjectException: No row with given identifier exists [ com.j2anywhere.users.Role#0]

            Note that this exception is only throws when an existing Role is associated with the new user. if the role does not exist the framework quite happily persist the user, it's role and the join table via manager.persist(user). Yet if the role already exists a exception is thrown.

            javax.ejb.EJBException: null; CausedByException is:
            detached entity passed to persist: com.j2anywhere.users.Role

            To re-attach the entity to the session we refresh it via manager.refresh(role) and then we are in a position to persist the user.

            It would be nice if the framework could perform this check for us, but at this stage it does not seem to be capable of checking if the contained objects are new or existing one. Also as you have seen from my previous emails doing :


            is a No-No. We will need to copy the individual roles. But this could be done via:

            Client code:
             User userA = new User();
             Role roleA = new Role();
             userA = userManager.createUser(userA);
             User userB = new User();
             Role foundRole = userManager.findRole("RoleA");
             System.out.println("Found Role : "+foundRole);

            Within the session bean user :
             public User createUser(User user)
             Iterator>Role< roles = user.getRoles().iterator();
             while (roles.hasNext())
             Role role = roles.next();
             catch (org.hibernate.UnresolvableObjectException e)
             System.err.println("Unable to refresh role : "+role);
             return user;

            Within the entity bean user :
            @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
             public Collection>Role< getRoles()
             return roles;
             public void setRoles(Collection >Role< roles)
             this.roles = roles;

            • 3. Re: Reassigned existing entities to other entities does not
              Emmanuel Bernard Master

              I don"'t know what you're talking about, just not share collection instances between 2 entities, it's a non sence from a relational perspective.