我有接下来的两个字段......
A类
@Entity
@Table(name="ClassA")
public class ClassA
{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
private Long id;
@Column(name = "Name", nullable = false, length = 50)
private String name;
@JsonManagedReference
@OneToMany(mappedBy="classA")
private List<ClassB> classBList;
}
B类
@Entity
@Table(name = "ClassB")
public class ClassB
{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
private Long id;
@Column(name = "Name", nullable = false, length = 50)
private String name;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "ClassAId", nullable = false)
private ClassA classA;
}
当我使用REST从数据库中获取所有ClassA记录时,结果就像魅力一样:
获取http://localhost:8080/REST/ClassA
[
{
"id": 1,
"name": "VVVV I",
"classB": [
{
"id": 1,
"name": "SSSS I"
},
{
"id": 2,
"name": "QQQQ II",
}
]
},
{
"id": 2,
"name": "RRRR II",
"classB": []
},
{
"id": 3,
"name": "FFFFF III",
"classB": []
},
{
"id": 4,
"name": "QWERR",
"classB": [
{
"id": 3,
"name": "GGGG III",
},
{
"id": 4,
"name": "HHHH IV",
},
{
"id": 5,
"name": "JJJJ V",
}
]
},
{
"id": 5,
"name": "TRRR",
"classB": []
}
]
问题是当我想获得ClassB列表时,ClassA根本没有返回......
获取http://localhost:8080/REST/ClassB
[
{
"id": 1,
"name": "SSSS I"
},
{
"id": 2,
"name": "QQQQ II"
},
{
"id": 3,
"name": "GGGG III"
},
{
"id": 4,
"name": "HHHH IV"
},
{
"id": 5,
"name": "JJJJ V"
}
]
在获取ClassB列表
时,我需要列出ClassA(其中没有classB)我如何实现这一目标?
祝你好运