Spring Data Rest: Multiple associations with the same relationship type detected

Regarding this question, I checked Spring's Exception Exception for Rest Data data , but could not get it to work for me.

As you can see in my code below, I added the @RestResource annotation with rel equal to some other value.

As in the previous question, POST requests work, but GET requests throw an exception due to multiple communication links to the same relationship type:

"Could not write JSON: several association associations were found with the same type relation! Disambiguate @ Org.springframework.data.rest.core.annotation.RestResource association (rel = CreatedBy, exported = true, path =, description=@org.springframework.data.rest.core.annotation.Descr iption ( value =)) @ javax.persistence.ManyToOne (optional = true, targetEntity = void, cascade = [], fetch = EAGER) @ Javax.persistence.JoinColumn (referencedColumnName = ASSIGNABLE_ID, nullable = false, unique = false, name = CREATEDBY , updatable = true, columnDefinition =, foreignKey=@javax.persistence.ForeignKey (name =, value = CONSTRAINT, foreignKeyDefinition =), table =, insertable = true) private com.ag.persistence.domain.PersonEntity com.ag.persistence.domain.TeamEntity .createdBy using @RestResource! (via the reference chain: org.springframework.hateoas.PagedResources [\ "_ embedded \"] β†’ java.util.UnmodifiableMap [\ "person century \ "] β†’ java.util.ArrayList [0]); nested exception com.fasterxml.jackson.databind.JsonMappingException: several association links with the same relationship type were found! Association Disambiguate @ Org.springframework.data.rest.core.annotation.RestResource (rel = CreatedBy, exported = true, path =, description=@org.springframework.data.rest.core.annotation.Descr iption (value =)) @ javax.persistence.ManyToOne (optional = true, targetEntity = void, cascade = [], fetch = EAGER) @ Javax.persistence.JoinColumn (referencedColumnName = ASSIGNABLE_ID, nullable = false, unique = false, name = CREATED_BY, updatable = true, columnDefinition =, foreignKey=@javax.persistence.ForeignKey (name =, value = CONSTRAINT, foreignKeyDefinition =), table =, insertable = true) private com.ag.persistence.domain.PersonEntity com.ag.persistence.domain.TeamEntity.createdBy using @RestResource! (via the reference chain: org.springframework.hateoas.PagedResources [\ "_ embedded \"] β†’ java.util.UnmodifiableMap [\ "person \"] β†’ java.util.ArrayList [0]) "

An error in this class occurs:

 @Entity @Table(name = "team") public class TeamEntity extends AssignableEntity { private String name; private LocalDateTime createdDate; private LocalDateTime modifiedDate; private Collection<MembershipEntity> memberships; private PersonEntity createdBy; private PersonEntity modifiedBy; @Basic @Column(name = "NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic @Column(name = "CREATED_DATE") public LocalDateTime getCreatedDate() { return createdDate; } public void setCreatedDate(LocalDateTime createdDate) { this.createdDate = createdDate; } @Basic @Column(name = "MODIFIED_DATE") public LocalDateTime getModifiedDate() { return modifiedDate; } public void setModifiedDate(LocalDateTime modifiedDate) { this.modifiedDate = modifiedDate; } @OneToMany(mappedBy = "team") public Collection<MembershipEntity> getMemberships() { return memberships; } public void setMemberships(Collection<MembershipEntity> memberships) { this.memberships = memberships; } @RestResource(rel = "team_createdBy") @ManyToOne @JoinColumn(name = "CREATED_BY", referencedColumnName = "ASSIGNABLE_ID", nullable = false) public PersonEntity getCreatedBy() { return createdBy; } public void setCreatedBy(PersonEntity createdBy) { this.createdBy = createdBy; } @RestResource(rel = "team_modifiedBy") @ManyToOne @JoinColumn(name = "MODIFIED_BY", referencedColumnName = "ASSIGNABLE_ID", nullable = false) public PersonEntity getModifiedBy() { return modifiedBy; } public void setModifiedBy(PersonEntity modifiedBy) { this.modifiedBy = modifiedBy; } } 

Ironically, I do not turn to this particular resource. I also have other resources with createdBy and modifiedBy - is this the one that causes this problem?

+7
java spring spring-data spring-data-rest spring-mvc
source share
2 answers

I suspect you might have exported = false on your PersonEntity object. Because of this, any links in PersonEntity will be added to the _links section of your TeamEntity . Since both PersonEntity have a createdBy link, they come across a TeamEntity.createdBy link.

To understand what is going on, this example may help:

 class Answer { Question q; //JPA ManyToOne back-reference } class Question { List<Answer> as; // JPA OneToMany reference } class UserAnswer { Answer a; Question q; } 

In my case, since Answer can only exist inside Question , our AnswerResource has the following: to prevent export of answers:

 @RestResource(exported = false) 

This results in the serialization of Answer objects in the parent object, and not as a link in the _links section, and this ultimately causes the problem ....

When UserAnswer serialized, it displays something like the following:

 { "answer" : { "creationDate" : "2014-09-18T17:28:31.000+0000", "modificationDate" : "2014-09-18T17:28:31.000+0000", "answerText" : "Vendas", }, "_links" : { "self" : { "href" : "http://localhost:9090/data/userAnswers/15" }, "question" : { "href" : "http://localhost:9090/data/userAnswers/15/question" } } } 

Note that the "_links.question" above is in Answer !

So, if you add Question to UserAnswer , you will see the error you are asking about, because UserAnswer also wants to include the _link link in Question .

In your case, I think your PersonEntity and your TeamEntity both have createdBy links.

I am not 100% sure what the solution is, I don’t know if you can specify the hierarchical names rel .

+2
source share

Short answer: add Spring Data Warehouses for ALL (or at least a larger number) of your objects. (In this case, it looks like you need to create a PersonRepository).

Long answer:

Spring Data Rest uses _links to avoid sending the entire JSON tree, but it can only do _links for mapped objects that have repositories.

If there is no repository for the object, Spring cannot use the link and instead just put the entire JSON object in the response.

I THINK what happens with this exception, is that relationships with a lower, second level are placed in _link, and more than one of them has the same name.

I am not 100% sure, I understand everything, but I had the same exception when I only had repositories for several entities, and when I created a repository for each individual object, the problem disappeared.

+1
source share

All Articles