Announcement Announcement Module
Collapse
No announcement yet.
Dynamic forms and the removal of objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic forms and the removal of objects

    Hi All,

    I've tried numerous different methods and searched the forum for this, but the only thing I can find about dynamic forms is how to add new rows using a LazyList.

    My problem: I have a parent object with a collection of child objects. That relation is mapped as a bi-directional one-to-many in Hibernate as this:

    Parent side:
    Code:
    <bag name="childs" lazy="false" inverse="true" cascade="all-delete-orphan" order-by="childId">
        <key column="parentId"/>
        <one-to-many class="Child"/>
    </bag>
    Child side:
    Code:
    <many-to-one name="parent" column="parentId" lazy="false" />
    Now the Parent class has this property and setter:
    Code:
    private List<Child> childs = LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(Child.class));
    
    [snip]
    
    public List<Child> getChilds() {
        return childs;
    }
    public void setChilds(List<Child> childs) {
        this.childs.clear();
        this.childs.addAll(childs);
    }
    So far so good, I have a LazyList and a custom setter to keep the LazyList. My form just has the usual child[${i.index}].property bindings, and I use the onFormChange(..) function to add new Child-rows to the form. Adding and updating the rows works as expected.

    I have a boolean property child.deleted, and in the onSubmit(..) function I iterate over the children to delete all with that propery from the Parent.childs list, and call child.setParent(null) to clean up (Hibernate should then delete the child from the database because of the cascade="all-delete-orphan" option).

    And now comes the problem: Nothing gets deleted

    I have debugged this a few times with different ideas, and the only conclusion I could get to was that Hibernate doesn't understand the replacement of it's "PersistentBag" with a LazyList implementation. (The PersistentBag is what Hibernate sends into the setChilds(..) method of the Parent.) And because of that it just ignores the removal of the Child from that LazyList.

    My question: Does anybody have this same kind of problem? And is there a best-practice method of implementing LazyList-like functionality while not upsetting Hibernate by replacing it's PersistentBag?

    P.S. More on Hibernate and changing collections: http://www.nearinfinity.com/blogs/pa..._play_who_owns

  • #2
    Originally posted by t0mas View Post
    I have debugged this a few times with different ideas, and the only conclusion I could get to was that Hibernate doesn't understand the replacement of it's "PersistentBag" with a LazyList implementation.
    I had no problem with the actual replacement, but I replaced only (Sorted)PersistentSet with (Sorted)Set. My only problem was that Hibernate really expected a SortedSet when it gave me a SortedPersistentSet.

    Originally posted by t0mas View Post
    And is there a best-practice method of implementing LazyList-like functionality while not upsetting Hibernate by replacing it's PersistentBag?
    Only alternative is probably implementing explicit actions like remove and add that trigger the corresponding changes on the Hibernate collections.

    Originally posted by t0mas View Post
    P.S. More on Hibernate and changing collections: http://www.nearinfinity.com/blogs/pa..._play_who_owns
    I always join you when it is about bashing Hibernate

    Joerg

    Comment


    • #3
      Originally posted by Jörg Heinicke View Post
      I had no problem with the actual replacement, but I replaced only (Sorted)PersistentSet with (Sorted)Set. My only problem was that Hibernate really expected a SortedSet when it gave me a SortedPersistentSet.
      I guess it has a problem with the replacement because of the cascade="all-delete-orphan" setting. I think Hibernate uses it's own collection to be able to monitor the removal of objects?

      Or maybe it is just that Hibernate doesn't understand the LazyList as an input, but that would be rather weird because the LazyList actually does implement the List interface..

      Originally posted by Jörg Heinicke View Post
      Only alternative is probably implementing explicit actions like remove and add that trigger the corresponding changes on the Hibernate collections.
      How would I implement the dynamic form using such an add-function? It seems that the "right" way to normally do that is using indexed binds for the form fields, but that only works with a LazyList.

      I'm wondering why there aren't more examples of this kind where people add, edit and delete rows in some parent-child relation.

      Comment


      • #4
        Originally posted by t0mas View Post
        I guess it has a problem with the replacement because of the cascade="all-delete-orphan" setting. I think Hibernate uses it's own collection to be able to monitor the removal of objects?

        Or maybe it is just that Hibernate doesn't understand the LazyList as an input, but that would be rather weird because the LazyList actually does implement the List interface.
        I would presume the the decorating of the List is the problem, but it would be a nice idea to remove it and confirm this.

        Hibernate "wraps" a java collection in an instance of PersistentCollection. This mechanism is designed to support tracking of changes to the collection's persistent state and lazy instantiation of collection elements. The downside is that only certain abstract collection types are supported and any extra semantics are lost
        http://www.hibernate.org/hib_docs/v3...ollection.html

        Comment

        Working...
        X