Skip to content

Conversion creates collection with invalid elements for @Controller bind target [SPR-6950] #11615

@spring-projects-issues

Description

@spring-projects-issues

Jean-Pierre Bergamin opened SPR-6950 and commented

See the referenced forum post for complete code coverage and log output.

In short.
Having two entities and a controller:

@Entity
public class Child {
  // ...
}

@Entity
public class Parent {
  @ManyToMany(cascade = CascadeType.ALL)
  public Set<Child> children = new HashSet<Child>();
  // ...
}

@RequestMapping(method = RequestMethod.PUT)
public String update(@Valid Parent parent, BindingResult result, ModelMap modelMap) {
  for(Child child : parent.getChildren()) { // ! this throws
    
  }
}

The Parent class has no static method findParent(Long id) that the IdToEntityConverter could use to convert a string to a Child entity.
So the conversion service tries its best to convert the info in the http request back into the HashSet "children", but inserts wrong elements into this collection. The result of the children collection is:

children = {
  new HashSet {
    "selectedId",
    "otherSelectedId"
  }
}

So instead of putting a Child entitiy with a given id into the set, the conversion service put another hashset with a string element into it.
This leads to very, very confusing (hibernate) exceptions when trying to persist the resulting entity again (https://forum.hibernate.org/viewtopic.php?f=1&t=1003029).


Affects: 3.0.1

Reference URL: http://forum.springsource.org/showthread.php?p=287402

Referenced from: commits d50881d, cbed1c1

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions