@ManyToMany使用EclipseLink 2.5.2和Jackson 2.4.4

时间:2015-01-06 14:30:02

标签: java spring jpa jackson eclipselink

在尝试更新实体时,我发出一个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]]

Category.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

中所述

User.java:

@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;

Role.java:

@ManyToMany(mappedBy = "roles")
private List<User> users;

当我将@JsonBackReference添加到Role.java中的users属性时,出现以下错误:

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 个答案:

答案 0 :(得分:1)

根据http://jackson-users.ning.com/forum/topics/deserializing-objects-with-bi-directional-many-to-many-relations,您需要在两个实体上使用@JsonIdentityInfo,而不是在映射上使用@JsonManagedReference和@JsonBackReference。