gpt4 book ai didi

java - Spring 数据休息 : Detected multiple association links with same relation type

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:22 26 4
gpt4 key购买 nike

关于这个问题,我查了一下Spring Data Rest Ambiguous Association Exception但无法让它为我工作。

正如您在下面的代码中看到的,我添加了 @RestResource 注释,其中 rel 等于其他值。

与上面的问题类似,POST 请求有效,但是 GET 请求抛出关于具有相同关系类型的多个关联链接的异常:

"Could not write JSON: Detected multiple association links with same relation type! Disambiguate association @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 using @RestResource! (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.UnmodifiableMap[\"persons\"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Detected multiple association links with same relation type! Disambiguate association @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 using @RestResource! (through reference chain: 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;
}
}

具有讽刺意味的是,我没有访问这个特定的资源。我还有其他关于 createdBymodifiedBy 的资源——是它导致了这个问题吗?

最佳答案

我怀疑您的 PersonEntity 对象上可能有 exported = false。因此,PersonEntity 中的任何引用都将添加到 TeamEntity_links 部分。因为两个 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;
}

在我的例子中,因为 Answer 只能存在于 Question 中,我们在 AnswerResource 上有以下内容,以防止 Answers正在导出:

@RestResource(exported = false)

这导致 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"
}
}
}

请注意上面的“_links.question”来自Answer!

因此,如果您将 Question 添加到 UserAnswer,您将看到您询问的错误,因为 UserAnswer 本身 想包含对问题的_link 引用。

在你的例子中,我认为你的 PersonEntity 和你的 TeamEntity 都有 createdBy 引用。

我还不是 100% 确定解决方案是什么,我不知道您是否可以指定分层 rel 名称。

关于java - Spring 数据休息 : Detected multiple association links with same relation type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com