JSON在select all中抛出java.lang.StackOverflowError
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON在select all中抛出java.lang.StackOverflowError相关的知识,希望对你有一定的参考价值。
您好,我正在写信给您
好吧,我有java ee实体MestechnologyGroup,它连接到MesTechnology。我正在使用JPA和hibernate,当我想从json数据库中提取数据(全选)时,浏览器会抛出我
java.lang.StackOverflowError的
这是MestechnologyGroup Entity
package pl.test.model;
import javax.persistence.*;
import java.util.Collection;
@Entity
public class Mestechnologygroup {
private Integer idTechnologyGroup;
private String name;
private String description;
private Integer number;
private Collection<Mestechnology> mestechnologiesByIdTechnologyGroup;
@Id
@Column(name = "idTechnologyGroup")
public Integer getIdTechnologyGroup() {
return idTechnologyGroup;
}
public void setIdTechnologyGroup(Integer idTechnologyGroup) {
this.idTechnologyGroup = idTechnologyGroup;
}
@Basic
@Column(name = "Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "Description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "Number")
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mestechnologygroup that = (Mestechnologygroup) o;
if (idTechnologyGroup != null ? !idTechnologyGroup.equals(that.idTechnologyGroup) : that.idTechnologyGroup != null)
return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (number != null ? !number.equals(that.number) : that.number != null) return false;
return true;
}
@Override
public int hashCode() {
int result = idTechnologyGroup != null ? idTechnologyGroup.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (number != null ? number.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "mestechnologygroupByIdTechnologyGroup")
public Collection<Mestechnology> getMestechnologiesByIdTechnologyGroup() {
return mestechnologiesByIdTechnologyGroup;
}
public void setMestechnologiesByIdTechnologyGroup(Collection<Mestechnology> mestechnologiesByIdTechnologyGroup) {
this.mestechnologiesByIdTechnologyGroup = mestechnologiesByIdTechnologyGroup;
}
}
和MesTechnology实体
package pl.test.model;
import javax.persistence.*;
import java.util.Collection;
@Entity
public class Mestechnology {
private Integer idTechnology;
private Integer number;
private String name;
private Boolean state;
private Collection<Mesattachmenttechnology> mesattachmenttechnologiesByIdTechnology;
private Collection<Mesoperation> mesoperationsByIdTechnology;
private Mestechnologygroup mestechnologygroupByIdTechnologyGroup;
@Id
@Column(name = "idTechnology")
public Integer getIdTechnology() {
return idTechnology;
}
public void setIdTechnology(Integer idTechnology) {
this.idTechnology = idTechnology;
}
@Basic
@Column(name = "number")
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "state")
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mestechnology that = (Mestechnology) o;
if (idTechnology != null ? !idTechnology.equals(that.idTechnology) : that.idTechnology != null) return false;
if (number != null ? !number.equals(that.number) : that.number != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (state != null ? !state.equals(that.state) : that.state != null) return false;
return true;
}
@Override
public int hashCode() {
int result = idTechnology != null ? idTechnology.hashCode() : 0;
result = 31 * result + (number != null ? number.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (state != null ? state.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "mestechnologyByIdTechnology")
public Collection<Mesattachmenttechnology> getMesattachmenttechnologiesByIdTechnology() {
return mesattachmenttechnologiesByIdTechnology;
}
public void setMesattachmenttechnologiesByIdTechnology(Collection<Mesattachmenttechnology> mesattachmenttechnologiesByIdTechnology) {
this.mesattachmenttechnologiesByIdTechnology = mesattachmenttechnologiesByIdTechnology;
}
@OneToMany(mappedBy = "mestechnologyByIdTechnology")
public Collection<Mesoperation> getMesoperationsByIdTechnology() {
return mesoperationsByIdTechnology;
}
public void setMesoperationsByIdTechnology(Collection<Mesoperation> mesoperationsByIdTechnology) {
this.mesoperationsByIdTechnology = mesoperationsByIdTechnology;
}
@ManyToOne
@JoinColumn(name = "idTechnologyGroup", referencedColumnName = "idTechnologyGroup", nullable = false)
public Mestechnologygroup getMestechnologygroupByIdTechnologyGroup() {
return mestechnologygroupByIdTechnologyGroup;
}
public void setMestechnologygroupByIdTechnologyGroup(Mestechnologygroup mestechnologygroupByIdTechnologyGroup) {
this.mestechnologygroupByIdTechnologyGroup = mestechnologygroupByIdTechnologyGroup;
}
}
TechnologyGroupRepositorum
package pl.test.repo;
import com.sun.istack.internal.NotNull;
import pl.test.model.Mestechnologygroup;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import java.util.List;
import static javax.transaction.Transactional.TxType.SUPPORTS;
@Transactional(SUPPORTS)
public class TechnologyGroupRepo {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testPU");
EntityManager em = entityManagerFactory.createEntityManager();
public List<Object[]> findById(@NotNull Integer id) {
TypedQuery<Object[]> query = em.createQuery("select t.name, t.number, t.description from Mestechnologygroup t where t.idTechnologyGroup =:id" , Object[].class)
.setParameter("id",id);
return query.getResultList();
}
public List<Mestechnologygroup> findAll() {
TypedQuery<Mestechnologygroup> query = em.createQuery("from Mestechnologygroup", Mestechnologygroup.class);
return query.getResultList();
}
//dodać dodawanie i deletowanie
}
TechnologyGroupEndpoint
package pl.test.rest;
import io.swagger.annotations.Api;
import pl.test.model.Mestechnologygroup;
import pl.test.model.Mesusers;
import pl.test.repo.TechnologyGroupRepo;
import pl.test.repo.UserRepo;
import javax.inject.Inject;
import javax.validation.constraints.Min;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@Api("technologygorup")
@Path("/tg")
public class TechnologyGroupEndpoint{
@Inject
private TechnologyGroupRepo technologyGroupRepo;
@GET
@Path("/{id : \d+}")
@Produces(APPLICATION_JSON)
public Response getTechnologyGroup(@PathParam("id") Integer id) {
Object[] mestechnologygroup = technologyGroupRepo.findById(id).toArray();
if (mestechnologygroup == null)
return Response.status(Response.Status.NOT_FOUND).build();
return Response.ok(mestechnologygroup).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTechnologyGroups() {
List<Mestechnologygroup> mestechnologygroups=technologyGroupRepo.findAll();
if (mestechnologygroups.size() == 0)
return Response.status(Response.Status.NO_CONTENT).build();
return Response.ok(mestechnologygroups).build();
}
}
服务器日志
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:31)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:104)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:63)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializeItem(AbstractContainerSerializer.java:134)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:52)
]]
浏览器引发的错误error我将不胜感激任何帮助
答案
MestechnologyGroup拥有MesTechnology的集合,后者参考了MestechnologyGroup。序列化时,它会序列化MestechnologyGroup,在那里找到MesTechnology进行序列化,再次找到MestechnologyGroup ...堆栈溢出。
在@ManyToOne上添加@JsonbTransient来停止它。
@JsonIgnore
@ManyToOne
@JoinColumn(name = "idTechnologyGroup", referencedColumnName = "idTechnologyGroup", nullable = false)
public Mestechnologygroup getMestechnologygroupByIdTechnologyGroup() {
return mestechnologygroupByIdTechnologyGroup;
}
从Mestechnology到Mesattachmenttechnology和Mesoperation都有@OneToMany。您还需要Json忽略这些实体中的@ManyToOne方面。
以上是关于JSON在select all中抛出java.lang.StackOverflowError的主要内容,如果未能解决你的问题,请参考以下文章