4 Replies Latest reply on Mar 13, 2007 10:51 PM by hanasakijiji

    ejb3 - setList(Set<MyObjs> list) - fails to persist.

    hanasakijiji

      reload the instance and call getList returns empty list.

      
      @OneToMany(
       cascade= {CascadeType.ALL, CascadeType.ALL}
       fetch=FetchType.EAGER)
      @JoinTable(
       name="myjointable",
       joinColumns={ @JoinColumn(name="owner_fk") }
       inverseJoinColumn=@JoinColumn(name="owned_fk")
      public Set<OwnedItem> getOwnedItems() {
       return(ownedItems);
      }
      
      private Set<OwnedItem> ownedItems = new HashSet<OwnedItem>();
      

      ////
      Q1: the cascade type in OneToMany can either be an array, as above, or a
      single item. Both compile. Both are in the hibernate annotations docs;
      however I do not find an explanation of the types or when to use an
      array and when not to (also how many items to put in the array and their
      meaning). Could someone explain this and point to reference docs?
      Q2: the above class was instantiated and persisted fine. Now it is
      loaded with an Query and the following is done:
      myObject.getOwnedItems.add(unsavedNewOwnedItem);
      em.flush();
      - Does the unsavedNewOwnedItem need to be persisted first?
      - what happens if it is not persisted yet?
      - what happens if it is already persisted?

      I have tried this withe the new owned item instanced already saved and
      not yet saved/persisted. The join table appears to be updated; however,
      when the class is reloaded, by a call to a new session bean (used by a
      subsequent call to a servlet), the Set is empty. It is expected to
      contain the item that was added.

        • 1. Re: ejb3 - setList(Set<MyObjs> list) - fails to persist.
          wolfgangknauf

          Hi !

          I will try to help:
          Q1:
          If you need only one cascade type value you use the single item notation:

          cascade=CascadeType.ALL

          You can OR the casace type values:
          The following sample will cascade any operation beside a delete (and here you will need the array notation):
          cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}



          Q2: I think you don't need to initialize the relationship field with a value. Set it to null, the container will do the rest.
          When you create relationship items you should update both sides of the relationship.
          Something like:
          myObject.getOwnedItems.add(unsavedNewOwnedItem);
          unsavedNewOwnedItem.setObject (myObject);


          Hope this helps

          Wolfgang

          • 2. Re: ejb3 - setList(Set<MyObjs> list) - fails to persist.
            hanasakijiji

            Unfortunately, the suggestion did not change the behavior. I have also double checked the "equals" method to be sure its working, and it is.

            Other thoughts? The same behavior is reproducable with:
            jboss 4.0.4, 4.0.5
            windows, linux
            jdk 1.5.x and 1.6.x
            postgresql and mysql

            • 3. Re: ejb3 - setList(Set<MyObjs> list) - fails to persist.
              wolfgangknauf

              Please post the relevant code of your beans.

              Wolfgang

              • 4. Re: ejb3 - setList(Set<MyObjs> list) - fails to persist.
                hanasakijiji

                Hi Wlfgang and all...

                I will try to get the code posted soon. In the meantime... I have narrowed things down. What do you make of this?
                Class1 actually has two getters as above.

                Set<OwnedItem1> getOwned1();
                Set<OwnedItem2> getOwned2();

                Each has has its own jointable and all are connected by the Integer Primary Keys of their respective tables

                Class1 - jointable1toOwned1 - Owned1
                Class1 - jointable1toOwned2 - Owned2

                It turns out that getOwned2 returns a Set with a maximum set.size of the number of items in getOwned1. Amost like an inner join where an outter join is required.