我有以下情况
@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
private Set<Child> children = new LinkedHashSet<Child>();
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne(fetch=FetchType.LAZY, optional = false)
private Parent parent;
}
每当我尝试使用 Form.form(Parent.class).bindFromRequest()将JSON解析为play的Form时,我会得到以下异常
Execution exception[[InvalidPropertyException: Invalid property 'children[0]' of bean class [models.Parent]: Illegal attempt to get property 'child' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'children[0]' of bean class [models.Parent]: Cannot get element with index 0 from Set of size 0, accessed using property path 'children[0]']]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at scala.Option.map(Option.scala:146) [scala-library-2.11.5.jar:na]
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'children[0]' of bean class [models.Parent]: Illegal attempt to get property 'children' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'children[0]' of bean class [models.Parent]: Cannot get element with index 0 from Set of size 0, accessed using property path 'children[0]'
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:848) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:571) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:548) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:912) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'children[0]' of bean class [models.Parent]: Cannot get element with index 0 from Set of size 0, accessed using property path 'children[0]'
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:800) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:571) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:548) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:912) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
我不想将LinkedHashSet更改为ArrayList(并将接口从Set更改为List),因为我在同一实体中有另一个OneToMany关系(这会导致 org.hibernate.loader.MultipleBagFetchException < /强>)...
我应该怎么做才能让BeanWrapperImpl处理集? 有可能吗?
EDITED: 我正在使用的JSON是:
{
"name": "PARENT",
"children": [
{
"name": "test"
}
]
}
非常感谢任何帮助。 提前谢谢