Problem with http binding of one-to-many relationship

222 views
Skip to first unread message

Sebastien Cesbron

unread,
Oct 21, 2011, 10:27:43 AM10/21/11
to play-fr...@googlegroups.com
Hi

I have a master-detail page with a master object and a list of children. I can in my page create/update/remove children and modify the master.

When I post my page, I get the master in my controller action method.

As I want my child objects to be deleted if removed from the list I have in my model :

@OneToMany(mappedBy="master", cascade=CascadeType.ALL, orphanRemoval=true)

As far as I understand, the JPAPlugin offers a binder that automatically re-attached the persistent object to the EM during the http bind phase. So in my controller method I get the attached master object on which I can call save method without merge.

My problem is that for the child, the binding phase does not replace the children in the list but replace hibernate managed list by a new arraylist. So when I try to save my master object I have an hibernate exception :

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

This seems a bug to me isn't it ?

It would be great if play can deeply reconcile jpa graphs

Regards
Seb

Oskari Porkka

unread,
Jan 19, 2012, 8:42:05 AM1/19/12
to play-fr...@googlegroups.com
I have the same problem. I'm saving rather complicated entities on a single form. The entity has a collection of subentities which in turn has again a collection of sub sub entities. It something like this:

class Entity extends Model {
    public String field;
    @OneToMany(cascade = CascadeType.ALL)
    public List<SubEntity> subEntities;
}

class SubEntity extends Model {
    public String subField;
    @OneToMany(cascade = CascadeType.ALL)
    public List<SubSubEntity> subSubEntities;
}

class SubSubEntity extends Model {
   public String subSubField;
}


My edit form uses Controller method update to save the entities:
public static void update(Entity entity) {
...
}

I send parameters like:
entity.id = 1;
entity.field = fieldValue;

entity.subEntity[0].id = 10
entity.subEntity[0].subField = subFieldValue0

entity.subEntity[1].id = 11
entity.subEntity[1].subField = subFieldValue1

entity.subEntity[0].subSubEntity[0].id = 100
entity.subEntity[0].subSubEntity[1].id = 101

entity.subEntity[1].subSubEntity[0].id = 110
entity.subEntity[1].subSubEntity[1].id = 111

.. and so on.

The JPAPlugin does good job in fetching the Entity[1] and its collections from the database and applying the change to entity.field. It however, cannot properly apply the changes to the collections. The entity.subEntities list is originally a Hibernate PersistentBag, but it is replaced by a regular ArrayList. Same happens to subSubEntities lists. This causes problems with saving, i.e. the changes are not saved on entity.save()

What do you think, is this a bug in JPAPlugin binding, which needs to be fixed? Or do I possibly have a too complicated approach for saving my entity and its sub entities and sub sub entities?
Reply all
Reply to author
Forward
0 new messages