【问题标题】:@ManyToMany with EclipseLink 2.5.2 and Jackson 2.4.4@ManyToMany 与 EclipseLink 2.5.2 和 Jackson 2.4.4
【发布时间】:2015-01-06 14:30:01
【问题描述】:

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

关系在 User 和 Role 中定义,如下所述:http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Collection_Mappings/ManyToMany

用户.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;

角色.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)

【问题讨论】:

  • 您的类路径中是否有旧版本的类,其测试映射不再有效?我看不出它将如何将用户角色引用关联到类别类。
  • 类别从未包含此关联。我认为它通过关系方案中的 contentList 和其他实体,直到它到达用户角色。
  • 现在我已将@JsonIgnore 添加到关系的两侧以使其工作。我分别获取用户/角色。

标签: java spring jpa jackson eclipselink


【解决方案1】:

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

【讨论】:

  • 对不起,但这不适合发布的问题。关于手册,在这种情况下(ManyToMany)我不需要@JsonBackReference,并且我已经对其进行了测试(有关详细信息,请参见问题末尾,之后没有添加)。
  • 文章底部提到了 JsonIdentityInfo 注释,这是你真正应该使用的注释,因为根据jackson-users.ning.com/forum/topics/…,集合不支持 JsonBackReference
猜你喜欢
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多