gpt4 book ai didi

json - RESTEasy 和 Jackson 提供程序无法将带有 @XmlIDREF 和 @XmlID 的实体序列化为 JSON

转载 作者:行者123 更新时间:2023-12-01 03:38:34 27 4
gpt4 key购买 nike

我有这样两个实体:

@XmlRootElement(name = "provider")
@XmlAccessorType(XmlAccessType.PROPERTY)
@Entity
public class Provider {
//...
@XmlElementWrapper(name = "industries")
@XmlElement(name = "industry")
@XmlIDREF
@ManyToMany(mappedBy = "providers", fetch = FetchType.EAGER)
public Set<Industry> getIndustries() {
return industries;
}

}

第二个实体:
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@Entity
public class Industry implements Serializable {

//...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "industry_id", nullable = false /*, columnDefinition = "BIGINT UNSIGNED" */)
public Long getIndustryId() {
return industryId;
}

public void setIndustryId(Long industryId) {
this.industryId = industryId;
}

//...

@XmlID
@Transient
public String getSelfLink() {
return getIndustryId().toString();
}
}

现在使用 RESTEasy 生成 XML 和 JSON 我只在 XML 中得到预期的结果,在 JSON 中,而不是 id 返回整个行业实体 + 抛出异常。

收到正确的 XML:
      <provider>
<userId>8</userId>
//.... - deleted for simplicity
<industries>
<industry>1</industry>
</industries>
</provider>

但 JSON 结果不正确:
{"provider":{ /* deleted for simplicity */ 
"industry":[{"industryId":1,"name":"Bran?a medyczna","description":null,"providers"}]}}

“行业”:1 应该只返回标识符!

抛出异常:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: pl.salonea.entities.Industry.providers, could not initialize proxy - no Session (through reference chain: pl.salonea.jaxrs.utils.ResourceList["resources"]->java.util.ArrayList[0]->pl.salonea.entities.Provider["industry"]->org.hibernate.collection.internal.PersistentSet[0]->pl.salonea.entities.Industry["providers"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)

和:
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: pl.salonea.entities.Industry.providers, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:88)

最佳答案

我遇到了同样的事情,我通过添加 @XmlAttribute 解决了它@XmlID的注释.这对我有用。 (注意:我的代码是用锡兰而不是 Java 编写的。)

shared entity class Employee(name, manager) {

xmlAttribute xmlID
generatedValue id
shared late Integer id;

column { length = 50; }
shared String name;

shared variable Integer? year = null;

xmlIDREF
manyToOne
shared Employee? manager;

xmlTransient
oneToMany { mappedBy = "manager"; }
shared Set<Employee> employees = set {};

}

我不知道为什么这是必要的,尤其是因为 AFAIK @XmlAttribute对比 @XmlElement否则对 JSON 没有意义。

关于json - RESTEasy 和 Jackson 提供程序无法将带有 @XmlIDREF 和 @XmlID 的实体序列化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32528790/

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