Json、JPA、ManytoOne 和 OneToMany => 递归
Json, JPA, ManytoOne and OneToMany => recursive
我已尝试阅读有关此主题的所有问题,但仍然缺少某些内容。
我的问题是:KIDS 和 PARENT 这两个表正在递归调用彼此
我有一个 EJB 要求一个带有 id 的 Parent,- 我(我认为)在需要的地方使用了 @OneToMany(mappedBy="parent")。结果可能会更好:当要求 parent(带有 id)时,它看到有一个孩子接受 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": [],
.....
类
我有一个无状态 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;
}
实体调用 Parents:
@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 有一个名为 FK-KIDS_parentsID
的 foreng 键
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.access0(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
@dcsohl 感谢您的帮助,- 我想我现在明白了...
我回到了我的问题,并再次尝试解决这个问题。这是 JSON 递归问题。
相反,我使用的是@XmlTransient,我找到了另一种方法来处理这个问题,- XMLTransient 的问题是你没有得到 id,- 所以我使用的是:@XmlIDREF and @XmlID解决这个问题。
@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")
@XmlAttribute @XmlID private int parentsid;
...
@ManyToMany
@JoinTable(name="PARENTS_KIDS",joinColumns=@JoinColumn(name ="PARENT_FK"), inverseJoinColumns=@JoinColumn(name = "KID_FK"))
private List<Kid> kids;
Class 孩子们
@Entity
@Table(name="KIDS")
@NamedQueries({
@NamedQuery(name="Kid.findAll", query="SELECT k FROM Kid k"),
})
@XmlRootElement
public class Kid implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="KIDSID")
private int kidsid;
@ManyToMany(mappedBy="kids")
@XmlAttribute @XmlIDREF private List<Parent> parents;
....
}
而且有效:D
如评论所示,这似乎不是 JPA 问题,而是 JSON 序列化问题。幸运的是,Jackson 提供了 several annotations 来处理这种情况。
使用@JsonManagedReference
on the parent and @JsonBackReference
on the child, and you may want to try playing with @JsonIdentityInfo
.
这里有一些 more reading 可能对您有所帮助。
这是循环依赖和 JSON 序列化的问题,解决这个问题的一个简单方法是在 类 上使用 @JsonIdentityInfo
,你应该有这样的东西:
@Entity
@Table(name="KIDS")
@NamedQuery(name="Kid.findAll", query="SELECT k FROM Kid k")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Kid implements Serializable {
和
@Entity
@Table(name="PARENTS")
@NamedQuery(name="Parent.findAll", query="SELECT p FROM Parent p")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Parent implements Serializable {
这样可以确保在每个孩子序列化中 parent 不会被序列化,但您只会得到他的 ID。
我已尝试阅读有关此主题的所有问题,但仍然缺少某些内容。
我的问题是:KIDS 和 PARENT 这两个表正在递归调用彼此
我有一个 EJB 要求一个带有 id 的 Parent,- 我(我认为)在需要的地方使用了 @OneToMany(mappedBy="parent")。结果可能会更好:当要求 parent(带有 id)时,它看到有一个孩子接受 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": [],
.....
类 我有一个无状态 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;
}
实体调用 Parents:
@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 有一个名为 FK-KIDS_parentsID
的 foreng 键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.access0(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
@dcsohl 感谢您的帮助,- 我想我现在明白了...
我回到了我的问题,并再次尝试解决这个问题。这是 JSON 递归问题。
相反,我使用的是@XmlTransient,我找到了另一种方法来处理这个问题,- XMLTransient 的问题是你没有得到 id,- 所以我使用的是:@XmlIDREF and @XmlID解决这个问题。
@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")
@XmlAttribute @XmlID private int parentsid;
...
@ManyToMany
@JoinTable(name="PARENTS_KIDS",joinColumns=@JoinColumn(name ="PARENT_FK"), inverseJoinColumns=@JoinColumn(name = "KID_FK"))
private List<Kid> kids;
Class 孩子们
@Entity
@Table(name="KIDS")
@NamedQueries({
@NamedQuery(name="Kid.findAll", query="SELECT k FROM Kid k"),
})
@XmlRootElement
public class Kid implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="KIDSID")
private int kidsid;
@ManyToMany(mappedBy="kids")
@XmlAttribute @XmlIDREF private List<Parent> parents;
....
}
而且有效:D
如评论所示,这似乎不是 JPA 问题,而是 JSON 序列化问题。幸运的是,Jackson 提供了 several annotations 来处理这种情况。
使用@JsonManagedReference
on the parent and @JsonBackReference
on the child, and you may want to try playing with @JsonIdentityInfo
.
这里有一些 more reading 可能对您有所帮助。
这是循环依赖和 JSON 序列化的问题,解决这个问题的一个简单方法是在 类 上使用 @JsonIdentityInfo
,你应该有这样的东西:
@Entity
@Table(name="KIDS")
@NamedQuery(name="Kid.findAll", query="SELECT k FROM Kid k")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Kid implements Serializable {
和
@Entity
@Table(name="PARENTS")
@NamedQuery(name="Parent.findAll", query="SELECT p FROM Parent p")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Parent implements Serializable {
这样可以确保在每个孩子序列化中 parent 不会被序列化,但您只会得到他的 ID。