Spring Data Rest Ambiguous Association Exception
Asked Answered
M

2

9

The newly added LinkCollectingAssociationHandler is throwing a MappingException due to an ambiguous association in my domain class.

The links array looks like this:

[<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"]

And it is trying to add another 'room' relation when it throws the exception.

The issue is that it seems to be adding links to relations which I have explicitly marked with @RestResource(exported = false)

Here is an example of a relation which I believe is causing this issue:

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

The type RoomsByUserAccessView has an embedded id made up of a Room and a User.

I have also annotated the embedded id property with:

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

and its properties like this:

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

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

public RoomsByUserAccessViewId() {
    //
}

How can I get it to ignore these relations properly when serializing to JSON?

My code was working prior to DATAREST-262 (https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550)

The full error message returned when I try to visit the rooms/ endpoint is as follows:

{ 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])" }

Measured answered 17/3, 2014 at 18:49 Comment(5)
Seems like a duplicate question, [check this one][1] #24427144 [1]: #24427144Onagraceous
hey, have you fixed this in any way? Im also trying to not show some values in json but the exception still is thrown : /Lieselotteliestal
I can't really remember... I believe that it was fixed in a more recent version of Spring Data Rest. At least I don't seem to have this problem anymore and @RestResource(exported = false) seems unnecessary since @JsonIgnore is being respected now.Measured
It is the @RestResource(exported = false) that causes the problem. It causes the associated links and body to be inlined into the parent resource, which can lead to rel collisions. It's still unclear to me whether that's the intended SDR behavior, but that's what's going on.Eck
I created a JIRA issue for this: jira.spring.io/browse/DATAREST-520Eck
T
5

I had a very similar problem . When adding a bidirectional relationship between two entities

I got an exception ""Could not write JSON: Detected multiple association links with same

relation type!" , While trying some solutions that i found here

(@JsonIgnore, @JsonIdentity, @RestResource, I also tried to do what Oliver offered )

(The relation was properly defined with @JsonManagedReference and @JsonBackReference)

Nothing helped.

At the end i managed to understand that spring data rest is trying to

figure out if the related entity is linkable ( while trying to build the json of the

requested entity )

(LinkCollectingAssociationHandler : doWithAssociation -> isLinkableAssociation)

, For doing that he is looking for a repository that deals with the

related entity. After adding a repository for the related entity.. works like a charm..

(I suppose that after adding a repo a mapping RepositoryAwareResourceInformation is being

created for this entity (that is what I saw at debug).

Taxiway answered 20/10, 2014 at 14:56 Comment(1)
Got tripped up by same scenario!Alveolus
R
0

I had this issue, and solved it as Ron suggests, but I thought I would expound a little. I didn't fully understand the first couple times I read Ron's answer...

@NodeEntity
public class Player extends Entity

@NodeEntity
public class PlayerTrade extends Entity

@NodeEntity
public class Trade extends Entity

I had repositories for Player and Trade, but none for PlayerTrade:

@RepositoryRestResource
public interface PlayerRepository  extends GraphRepository<Player> {

@RepositoryRestResource
public interface TradeRepository extends GraphRepository<Trade> {

As soon as I added the last repo it worked.

@RepositoryRestResource
public interface PlayerTradeRepository extends GraphRepository<PlayerTrade> 

I tried using @RestResource with rel or excluded, but couldn't get it dialed in. Does this mean that every entity in our JSON graph must have a repository?

Reseat answered 6/1, 2015 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.