JPA:Select parent 和 child 具有一对多关系
JPA : Select parent and child with oneToMany relatinships
实现了一对多关系 select parent 和 child
我有 onetomany
关系,我想查询 (select *)
Folders.java
@Entity
@Table(name = "FOLDERS")
public class Folders implements Serializable {
@Id
@Column(name = "id")
Long id;
@ManyToOne
@JoinColumn(name = "Folder_Author", insertable=false, updatable=false)
private Authors author;
// Getter + setter
}
Authors.java
@Entity
@Table(name = "AUTHORS")
public class Authors implements Serializable {
@Id
@Column(name = "id")
Long id;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
private List<Folders> folders = new ArrayList<Folders>();
}
我的要求:
Query query = em.createQuery("SELECT f FROM Folders f");
return query.getResultList();
我得到了这个结果:
[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
{"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
..
..
}
]
我的代码有什么问题?当我执行查询时出现什么问题我得到了这个结果.. .. 我想得到这个结果
[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
}
}
]
只需在 collection 字段中使用 @JsonIgnore
,如下所示:
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
@JsonIgnore
private List<Folders> folders = new ArrayList<Folders>();
在作者的文件夹列表上使用@JsonBackReference class 以防止递归序列化。
另见
实现了一对多关系 select parent 和 child
我有 onetomany
关系,我想查询 (select *)
Folders.java
@Entity
@Table(name = "FOLDERS")
public class Folders implements Serializable {
@Id
@Column(name = "id")
Long id;
@ManyToOne
@JoinColumn(name = "Folder_Author", insertable=false, updatable=false)
private Authors author;
// Getter + setter
}
Authors.java
@Entity
@Table(name = "AUTHORS")
public class Authors implements Serializable {
@Id
@Column(name = "id")
Long id;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
private List<Folders> folders = new ArrayList<Folders>();
}
我的要求:
Query query = em.createQuery("SELECT f FROM Folders f");
return query.getResultList();
我得到了这个结果:
[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
{"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
"folders":[
..
..
}
]
我的代码有什么问题?当我执行查询时出现什么问题我得到了这个结果.. .. 我想得到这个结果
[
{
"id":29,
"noFolder":"0017",
"author":{
"id":9,
"name":"Alpha",
"service":null,
}
}
]
只需在 collection 字段中使用 @JsonIgnore
,如下所示:
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
@JoinColumn(name = "Folder_Author", referencedColumnName = "id", nullable=false)
@JsonIgnore
private List<Folders> folders = new ArrayList<Folders>();
在作者的文件夹列表上使用@JsonBackReference class 以防止递归序列化。
另见