2 Replies Latest reply on Dec 12, 2008 7:44 AM by nschweig

    Which methods should an Entity contain?

    nschweig

      Hi,

      after reading a lot and seeing some examples I have a question what an entity bean is allowed to contain.

      I have one entity "User" that has a relation to the entity "Role". The question is, can I implement methods like addRole() and removeRole() here or do I have to do this in the session beans? Which is the best way?
      I saw a lot of entities that only contained the setter/getter for their attributes.

      Thank you,
      Nicki

        • 1. Re: Which methods should an Entity contain?
          wolfgangknauf

          Hi Nicki,

          My 2 cent: I would suggest to place "addRole" and "removeRole" in the session bean. The reason is that for relationships, both sides of the relation must be modified:

          For example this snippet will add a new user-to-role-connection:

          myUser.getRoles().add (myRole);
          myRole.getUsers().add (myUser);


          Placing this in an entity breaks data encapsulation principles, because the entity manipulates other entitities ;-). But technically, it would work in the entity, too. It is just "dirty".

          Best regards

          Wolfgang

          • 2. Re: Which methods should an Entity contain?
            nschweig

            Hi,

            thank you for your response.
            I thought that, too but were not sure.

            Thanks, Nicki