1 Reply Latest reply on Feb 24, 2006 6:06 AM by instinct

    Collection Access Best Practices

    treespace Novice

      I want a persistent collection but I do not want anybody accessing the collection directly. So rather than getProtons you might call addProton:

      
      @Entity class Atom
      {
       ...
       public void addProton(Proton proton)
       {
       protons.add(proton);
       proton.setAtom(this);
       }
      
       public List getProtons() ...
       ...
      
      


      Can my getProtons... property method be declared private? Use of the encapsulated collection access methods cannot otherwise be enforced.



        • 1. Re: Collection Access Best Practices
          Newbie

           

          ejb-3_0-pfd-spec-persistence.pdf wrote:

          If the entity has field-based access, the persistence provider runtime accesses instance variables directly.


          If the persistence provider does not use the getters and setters I don't see why your accessor methods should not be declared private.

          I changed
          public Collection<Contact> getContact() {
           return contact;
          }
          


          to
          private Collection<Contact> getContact() {
           return contact;
          }