5 Replies Latest reply on Jun 14, 2010 8:27 AM by tiagoemerick

    How to persist collection attribute

    girlcoder

      Hi


      Is there any special way to persist entity with Set attribute (many-to-many relationsip)?


      Works fine without setting this attribute , but if I add something I get exception:



      org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions



      Please Help Me


      Agnieszka

        • 1. Re: How to persist collection attribute
          dthibau1

          Your problem here is that your attribute is associated with 2 Hibernate's session !!


          (perharps in 2 seam's component ?)


          • 2. Re: How to persist collection attribute
            girlcoder

            How to check if I have 2 sessions ?



            I when I changed the way to add to Set attribute
            I had another exception here:


            Different object with the same identifier value was already associeted with the session



            Have you got any idea ?


            • 3. Re: How to persist collection attribute

              Without seeing the entity code, I would guess that what you need is a CascadeType.PERSIST on the parent entity.


              i.e:


              private Set<T> myStuff;
              
              @ManyToMany(cascade={CascadeType.PERSIST})
              public Set<T> getMyStuff() {
                  return myStuff;
              }
              
              public void setMyStuff(Set<T> newStuff) {
                  myStuff = newStuff;
              }
              



              This should cause any changes to the sub entity to be persisted when the parent entity is.


              Hope this helps


              g

              • 4. Re: How to persist collection attribute
                girlcoder

                I have this on first entity:


                
                 private Set<CbShortCode> cbShortCodes = new HashSet<CbShortCode>(0);
                
                @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cbServices") 
                     public Set<CbShortCode> getCbShortCodes() {
                          return this.cbShortCodes;     
                     }
                
                     public void setCbShortCodes(Set<CbShortCode> cbShortCodes) {
                          this.cbShortCodes = cbShortCodes;
                          
                     }
                


                and this on second:


                @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
                     @JoinTable(name = "cb_shoco_services", schema = "public", joinColumns = { @JoinColumn(name = "addr", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "service_id", nullable = false, updatable = false) })
                     public Set<CbService> getCbServices() {
                          return this.cbServices;
                     }
                
                     public void setCbServices(Set<CbService> cbServices) {
                          this.cbServices = cbServices;
                     }
                



                generated from existng database - Seam Generate Entities


                do I have to change something here ?

                • 5. Re: How to persist collection attribute
                  tiagoemerick

                  Hi guys.


                  sorry for revive an old topic, but i do not understand how this problem occurs in seam.


                  David THIBAU, you said that my attribute is associated with 2 Hibernate's sessions.
                  how it's possible when seam is managing my sessions?




                  David THIBAU wrote on Dec 15, 2008 14:45:


                  Your problem here is that your attribute is associated with 2 Hibernate's session !!

                  (perharps in 2 seam's component ?)




                  Click HELP for text formatting instructions. Then edit this text and check the preview.