gpt4 book ai didi

java - Json、JPA、ManytoOne 和 OneToMany => 递归

转载 作者:行者123 更新时间:2023-12-01 14:08:37 37 4
gpt4 key购买 nike

我已尝试阅读有关此主题的所有问题,但仍然缺少某些内容。

我的问题是:两个表 KIDS 和 PARENT 正在递归调用彼此

我有一个 EJB 要求一个带有 id 的父级, - 我(我认为)在需要的地方使用了 @OneToMany(mappedBy="parent")。结果可能会更好:当要求 parent (带有 id)时,它看到有一个 child 带走了要求 child 的 parent ......(在一个几乎永无止境的故事中:)

JSON 输出

{
"parentsid": 1,
...
"parentsusername": null,
"calendars": [],
"kids": [
{
"kidsid": 2,
...
"parent": {
"parentsid": 1,
...
"calendars": [],
"kids": [
{
"kidsid": 2,
"calendars": [],
"checkins": [],
"parent": {
"parentsid": 1,
"calendars": [],
"kids": [
{
"kidsid": 2,
"calendars": [],
.....

类(class)
我有一个无状态 Bean 调用:
@Stateless
@LocalBean
@Path("/checkin")
public class CheckinService {

@PersistenceContext(unitName="chkin",type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
public Parent read(@PathParam("id") int id) {
Parent parent = entityManager.find(Parent.class, id);
return parent;
}

实体类是通过以下方式制作的
/**
* The persistent class for the KIDS database table.
*/
@Entity
@Table(name="KIDS")
@NamedQuery(name="Kid.findAll", query="SELECT k FROM Kid k")
public class Kid implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="KIDSID")
private int kidsid;

//bi-directional many-to-one association to Calendar
@OneToMany(mappedBy="kid")
private List<Calendar> calendars;

//bi-directional many-to-one association to Checkin
@OneToMany(mappedBy="kid")
private List<Checkin> checkins;

//bi-directional many-to-one association to Parent
@ManyToOne **// <=============**
@JoinColumn(name="parentsId")
private Parent parent;
}

实体呼吁家长:
@Entity
@Table(name="PARENTS")
@NamedQuery(name="Parent.findAll", query="SELECT p FROM Parent p")
public class Parent implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="PARENTSID")
private int parentsid;

...

//bi-directional many-to-one association to Calendar
@OneToMany(mappedBy="parent")
private List<Calendar> calendars;

//bi-directional many-to-one association to Kid
@OneToMany(mappedBy="parent") **// <=============**
private List<Kid> kids;

数据库看起来像这样:
两个数据库表,一个名为 PARENTS,一个名为 KIDS 有一个从 PARENTS 到 KIDS 的 onetomany,带有一个名为 FK-KIDS_parentsID 的 foreng key
PARENTS ==========FK_KIDS_parentsId==========< KIDS
(PK) PARENTSID (PK) KIDSID
(FK) parentsId

堆栈跟踪
[error occurred during error reporting (printing native stack), id 0xc0000005]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(JI[I[I[IJ)I+0
j sun.nio.ch.WindowsSelectorImpl$SubSelector.poll()I+43
j sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Lsun/nio/ch/WindowsSelectorImpl$SubSelector;)I+1
j sun.nio.ch.WindowsSelectorImpl.doSelect(J)I+63
j sun.nio.ch.SelectorImpl.lockAndDoSelect(J)I+37
j sun.nio.ch.SelectorImpl.select(J)I+30
j sun.nio.ch.SelectorImpl.select()I+2
j org.xnio.nio.SelectorUtils.await(Lorg/xnio/nio/NioXnio;Ljava/nio/channels/SelectableChannel;I)V+23
j org.xnio.nio.NioSocketConduit.awaitWritable()V+26
j org.xnio.conduits.AbstractSinkConduit.awaitWritable()V+7
j io.undertow.conduits.ChunkedStreamSinkConduit.awaitWritable()V+7
j org.xnio.conduits.ConduitStreamSinkChannel.awaitWritable()V+4
j io.undertow.channels.DetachableStreamSinkChannel.awaitWritable()V+20
j io.undertow.server.HttpServerExchange$WriteDispatchChannel.awaitWritable()V+20
j org.xnio.channels.Channels.writeBlocking(Ljava/nio/channels/GatheringByteChannel;[Ljava/nio/ByteBuffer;II)J+34
j io.undertow.servlet.spec.ServletOutputStreamImpl.write([BII)V+311
j org.jboss.resteasy.plugins.server.servlet.HttpServletResponseWrapper$DeferredOutputStream.write([BII)V+15
j org.jboss.resteasy.util.CommitHeaderOutputStream.write([BII)V+11
j com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer()V+24
j com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeBytes([B)V+17
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979c78 [0x00000000029796c0+0x5b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979b78 [0x00000000029796c0+0x4b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41
j com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(Ljava/util/List;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+163
j com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+7
j com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+34
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979b78 [0x00000000029796c0+0x4b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979b78 [0x00000000029796c0+0x4b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41
j com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(Ljava/util/List;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+163
j com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+7
j com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+34
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979b78 [0x00000000029796c0+0x4b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41
J 1041 C2 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V (185 bytes) @ 0x0000000002979b78 [0x00000000029796c0+0x4b8]
j com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(Ljava/lang/Object;Lcom/fasterxml/jackson/core/JsonGenerator;Lcom/fasterxml/jackson/databind/SerializerProvider;)V+41

最佳答案

正如评论所示,这似乎不是 JPA 问题,而是 JSON 序列化问题。幸运的是,Jackson 提供了several annotations来应对这种情况。

使用 @JsonManagedReference 在父级和 @JsonBackReference 上在 child 身上,你可能想试试玩 @JsonIdentityInfo .

这是一些 more reading你可能会觉得有帮助。

关于java - Json、JPA、ManytoOne 和 OneToMany => 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30589257/

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