Spring Data Rest Ambiguous Association Exception

时间:2014-03-17 18:49:46

标签: json spring spring-mvc spring-data-rest spring-hateoas

由于我的域类中存在模糊关联,新添加的LinkCollectingAssociationHandler会引发MappingException

links数组如下所示:

[<http://localhost:8080/rooms/2/roomGroups>;rel="roomGroups", <http://localhost:8080/rooms/2/userGroup>;rel="userGroup", <http://localhost:8080/rooms/2/room>;rel="room", <http://localhost:8080/rooms/2/originatingConferences>;rel="originatingConferences", <http://localhost:8080/rooms/2/user>;rel="user"]

它正试图增加另一个房间&#39;抛出异常的关系。

问题在于它似乎添加了我明确标有@RestResource(exported = false)

的关系链接

以下是我认为导致此问题的关系示例:

@JsonIgnore
@RestResource(exported = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.room", cascade = {CascadeType.REMOVE})
private Set<RoomsByUserAccessView> usersThatCanDisplay = new HashSet<>(); 

RoomsByUserAccessView类型的嵌入式ID由RoomUser组成。

我还用以下内容注释了嵌入式id属性:

@JsonIgnore
@RestResource(exported = false)
private RoomsByUserAccessViewId pk = new RoomsByUserAccessViewId();

及其属性如下:

@JsonIgnore
@RestResource(exported = false)
private Room room;

@JsonIgnore
@RestResource(exported = false)
private User userWithAccess;

public RoomsByUserAccessViewId() {
    //
}

如何在序列化为JSON时正确忽略这些关系?

我的代码在DATAREST-262之前工作(https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550

当我尝试访问房间/终端时返回的完整错误消息如下:

{ timestamp: "2014-03-17T13:38:05.481-0500" error: "Internal Server Error" status: 500 exception: "org.springframework.http.converter.HttpMessageNotWritableException" message: "Could not write JSON: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Detected multiple association links with same relation type! Disambiguate association @com.fasterxml.jackson.annotation.JsonIgnore(value=true) @javax.persistence.ManyToOne(fetch=EAGER, cascade=[], optional=true, targetEntity=void) @org.springframework.data.rest.core.annotation.RestResource(description=@org.springframework.data.rest.core.annotation.Description(value=), path=, exported=false, rel=) private com.renovo.schedulerapi.domain.Room com.renovo.schedulerapi.domain.RoomsByUserAccessViewId.room using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources["content"]->java.util.UnmodifiableCollection[0])" }

2 个答案:

答案 0 :(得分:5)

我有一个非常类似的问题。在两个实体之间添加双向关系时

我收到异常“”无法编写JSON:检测到多个关联链接

关系类型!“,在尝试我在这里找到的一些解决方案时

@JsonIgnore, @JsonIdentity, @RestResource,我也尝试做Oliver offered

(该关系已使用@JsonManagedReference@JsonBackReference

正确定义

没有任何帮助。

最后,我设法理解Spring数据正在尝试

确定相关实体是否可链接(在尝试构建

的json时)

请求的实体)

LinkCollectingAssociationHandler : doWithAssociation -> isLinkableAssociation

,为此,他正在寻找一个处理

的存储库

相关实体。为相关实体添加存储库后,就像魅力一样......

(我想在添加一个repo后,一个映射RepositoryAwareResourceInformation正在

为此实体创建(这是我在调试时看到的)。

答案 1 :(得分:0)

我有这个问题,并按照罗恩的建议解决了这个问题,但我想我会解释一下。我没有完全理解前几次我读罗恩的回答......

@NodeEntity
public class Player extends Entity

@NodeEntity
public class PlayerTrade extends Entity

@NodeEntity
public class Trade extends Entity

我有玩家和交易的存储库,但没有PlayerTrade的存储库:

@RepositoryRestResource
public interface PlayerRepository  extends GraphRepository<Player> {

@RepositoryRestResource
public interface TradeRepository extends GraphRepository<Trade> {

一旦我添加了最后一个回购,它就有效了。

@RepositoryRestResource
public interface PlayerTradeRepository extends GraphRepository<PlayerTrade> 

我尝试使用带有rel或者排除的@RestResource,但无法将其拨入。这是否意味着我们的JSON图中的每个实体都必须有一个存储库?