gpt4 book ai didi

mysql - 将 JAXB(在 JAX-RS 中)与包含 ManyToMany、OneToMany 关系的 JPA 实体一起使用

转载 作者:行者123 更新时间:2023-11-29 05:16:09 25 4
gpt4 key购买 nike

我在尝试编写 JAX-RS 资源时遇到了问题。当我将 @XmlRootElement 添加到需要使用 JAXB 和 RestEasy 序列化为 XML/JSON 的 JPA 实体时。

已抛出的错误:

 Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: pl.salonea.entities.Provider.industries, 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)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:6

还有:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: pl.salonea.entities.Provider.industries, could not initialize proxy - no Session (through reference chain: pl.salonea.jaxrs.utils.ResourceList["resources"]->java.util.ArrayList[6]->pl.salonea.entities.Provider["industries"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:190)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:671)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeWithType(BeanSerializerBase.java:549)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:134)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:94)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:575)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:851)

这是实体属性及其注释的代码:

@ManyToMany(mappedBy = "providers")
public Set<Industry> getIndustries() {
return industries;
}

public void setIndustries(Set<Industry> industries) {
this.industries = industries;
}

更新

我考虑如何实现这种 JSON 响应模式:

   {
"description": "Provider 1",
"attr1": 0,
"attr2": 2,
"attrN": "auction1.jpg",
"industries": [
{
"link": {
"href": "http://localhost:8080/rest/v1.0/industries/5",
"method": "GET",
"rel": "self"
}
},
{
"link": {
"href": "http://localhost:8080/rest/v1.0/industries/6",
"method": "GET",
"rel": "self"
}
}
]
}

最佳答案

如果您不需要序列化行业,您可以使用@XmlTransient 注释 setter/getter ...否则您可以在@ManyToMany 注释中将获取属性设置为EAGER,以防您知道每个提供者不会有很多行业。如果您选择该路径,并且如果您在 Industry 实体中有一个返回 Provider 的指针,则需要使用 @XmlTransient 对其进行注释,以避免 jaxb 序列化程序进入无限循环。

关于mysql - 将 JAXB(在 JAX-RS 中)与包含 ManyToMany、OneToMany 关系的 JPA 实体一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32512716/

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