java@ManyToMany与EclipseLink 2.5.2和Jackson 2.4.4
在尝试更新实体时,我触发了一个REST请求:PUT <ip>/categories/5
,其中包含一个JSON主体:{"id":"4","name":"otherCategory","contentList":[]}
错误消息是:
WARN [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]
(default task-27) Failed to evaluate deserialization for type [simple type, class
com.example.domain.assetmanagement.Category]: java.lang.IllegalArgumentException:
Can not handle managed/back reference 'users-roles': no back reference property
found from type [collection type; class java.util.List, contains [simple type,
class com.example.domain.user.permission.Role]]
类别。java不包含此关系,它只有三个属性id、name和contentList
@Id
@GeneratedValue(generator = "UUID_CATEGORY")
private String id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "category")
@JsonManagedReference("category-content")
private List<Content> contentList;
[+getters/setters]
关系在用户和角色中定义,如所述:http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany
用户。爪哇:
@ManyToMany
@JoinTable(name = "user_role",
joinColumns = {
@JoinColumn(
name = "user_id",
referencedColumnName = "id"
)
},
inverseJoinColumns =
@JoinColumn(
name = "role_id",
referencedColumnName = "id"
)
)
@JsonManagedReference("users-roles")
private List<Role> roles;
角色。爪哇:
@ManyToMany(mappedBy = "roles")
private List<User> users;
当我将@JsonBackReference添加到角色中的users属性时。java,我得到以下错误:
WARN [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]
(default task-28) Failed to evaluate deserialization for type [simple type, class
com.example.domain.assetmanagement.Category]: java.lang.IllegalArgumentException:
Can not handle managed/back reference 'users-roles': back reference type
(java.util.List) not compatible with managed type (com.example.domain.user.User)
# 1 楼答案
根据http://jackson-users.ning.com/forum/topics/deserializing-objects-with-bi-directional-many-to-many-relations,您需要在两个实体上使用@JsonIdentityInfo,而不是在映射上使用@JsonManagedReference和@JsonBackReference