Cha*_*had 7 java spring spring-mvc spring-data spring-data-rest
关于这个问题,我检查了Spring Data Rest Ambiguous Association Exception但是无法让它为我工作.
正如您在下面的代码中看到的,我添加了@RestResource与rel其他值相等的注释.
与上面的问题类似,POST请求有效,但GET请求会抛出具有相同关系类型的多个关联链接的异常:
"无法编写JSON:检测到具有相同关系类型的多个关联链接!消除关联@ org.springframework.data.rest.core.annotation.RestResource(rel = createdBy,exported = true,path =,description =@org.springframework. data.rest.core.annotation.Description(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使用@RestResource!(通过引用链:org.springframework.hateoas.PagedResources [\"_ embedded \"] - > java.util.UnmodifiableMap [\"people \"] - > java.util.ArrayList [0]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:检测到多个关联 具有相同关系类型的链接!消除关联@ org.springframework.data.rest.core.annotation.RestResource(rel = createdBy,exported = true,path =,description = @ org.springframework.data.rest.core.annotation.Description(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使用@RestResource!(通过参考链:org.springframework.hateoas.PagedResources [\"_ embedded \"] - > java.util.UnmodifiableMap [\"persons \"] - > java.util.ArrayList [0])"
这个错误似乎发生在这个类中:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
具有讽刺意味的是,我没有访问此特定资源.我还有其他资源createdBy和modifiedBy- 是导致这个问题的那个?
我怀疑你可能有exported = false你的PersonEntity对象。因此,任何引用都PersonEntity将添加到_links您的TeamEntity. 因为两个PersonEntity对象都有一个createdBy引用,所以它们与该引用发生碰撞TeamEntity.createdBy。
要了解正在发生的情况,此示例可能会有所帮助:
class Answer {
Question q; //JPA ManyToOne back-reference
}
class Question {
List<Answer> as; // JPA OneToMany reference
}
class UserAnswer {
Answer a;
Question q;
}
Run Code Online (Sandbox Code Playgroud)
就我而言,因为 anAnswer只能存在于 a 中Question,所以我们在 , 上有以下内容AnswerResource,以防止导出答案:
@RestResource(exported = false)
Run Code Online (Sandbox Code Playgroud)
这会导致Answer对象在父对象中序列化,而不是作为该_links部分中的引用,这最终成为问题的原因......
序列化后UserAnswer,它会呈现如下内容:
{
"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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,上面的“_links.question”来自Answer!
因此,如果您将 a 添加Question到UserAnswer,您将看到您所询问的错误,因为它UserAnswer本身也希望包含对 a 的 _link 引用Question。
就你而言,我认为你PersonEntity和你TeamEntity都有createdBy参考资料。
我还不是 100% 确定解决方案是什么,我不知道您是否可以指定分层rel名称。
| 归档时间: |
|
| 查看次数: |
1968 次 |
| 最近记录: |