2015-01-06 39 views
0

試圖更新一個實體,我火了REST請求:PUT <ip>/categories/5用JSON體:{"id":"4","name":"otherCategory","contentList":[]}@ManyToMany用的EclipseLink 2.5.2和2.4.4傑克遜

錯誤消息是:

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,名稱和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; 

當我在Role.java添加@JsonBackReference到用戶的財產,我得到以下錯誤:

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) 
+0

您可以使用不再有效的測試映射在您的類路徑上使用舊版本的類嗎?我不明白它會如何將用戶角色引用與類別類關聯。 – Chris

+0

類別從未包含此關聯。我認爲它通過關係模式中的contentList和其他實體,直到它到達用戶角色。 – dexBerlin

+0

現在我已經將@JsonIgnore添加到關係的兩側以使其工作。我分別獲取用戶/角色。 – dexBerlin

回答

1

根據http://jackson-users.ning.com/forum/topics/deserializing-objects-with-bi-directional-many-to-many-relations您需要在bot上使用@JsonIdentityInfo h實體,而不是映射上的@JsonManagedReference和@JsonBackReference。

+1

對不起,但這不適合發佈的問題。在這種情況下,我不需要'@ JsonBackReference'(ManyToMany),並且我已經對它進行了測試(請參閱問題末尾以獲取詳細信息,之後未添加)。 – dexBerlin

+0

本文底部提到了JsonIdentityInfo註釋,這是您真正應該使用的,因爲根據http://jackson-users.ning.com/forum/topics/how-to-handle-循環引用-IN-A-雙向一對多一對多 – Chris