Spring Data Rest Ambiguous Association Exception

天大地大妈咪最大 提交于 2019-12-05 02:04:46
Hezi Schrager

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

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!