OneToMany 和 ManyToOne 实体 objects 在递归中链接
OneToMany and ManyToOne entities objects are linked in recursion
在我的 java-spring 引导应用程序中,我遇到了 2 java class 与注释 @oneToMany 和 @ManyToOne 的 parent-children 双向关系的问题。
当我尝试将它们保存在数据库中并向控制器端点发送 post 请求时,会在两个 object 之间创建一个递归循环。
在controller中调用@getMapping(/{fatherId})方法也出现同样的问题
基本上 java 收到父亲 object 包含一组 Children 引用他们的父亲 object ,其中包含他们的 children榜等等等等..
我该如何解决这个问题?我看到一些类似的 post 建议使用 Jackson 库注释,是否绝对有必要将它添加到 pom 中?我更喜欢在我的 pom 中引用基本的最小包集,而 Jackson 还没有在使用 Spring Initializer(spring boot).
编写的默认包中
谢谢大家的建议!
parentclass是父亲:
@Entity
@Table(name="Father")
public class Father {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(mappedBy="father", fetch = FetchType.LAZY)
private Set<Child> children;
一个父亲可以有多个child人,但一个child只能有一个父亲。
childclass:
@Entity
@Table(name="Child")
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name ="ID_FATHER")
private Father father;
控制器中的端点是这样的:
@Transactional
@PostFather(path = "/newFather", consumes = "application/json", produces = "application/json")
public ResponseEntity<?> createNewFather(@Valid @RequestBody Father father, BindingResult result) {
Set<Child> childrenSet = new HashSet<Child>();
//ArrayList<Child> newChildren= new ArrayList<Child>();
//newChildren.addAll(father.getChildren());
for (Child c : father.getChildren()) {
Child newChild = new Child();
newChild.setId(c.getId());
newChild.setName(c.getName());
newChild.setFather(father);
fatherService.saveOrUpdateChild(newChild);
childrenSet.add(newChild);
}
father.setChildren(childrenSet);
Father newFather = fatherService.saveOrUpdateFather(father);
return new ResponseEntity<Father>(newFather, HttpStatus.CREATED);
}
@GetFather("/father/{fatherId}")
public ResponseEntity<?> getFatherById(@PathVariable int fatherId) {
Father father = fatherService.findFatherById(fatherId);
for (Child c : father.getChildren()) {// !!workaround to stop the loop!!
c.setFather(null);
}
return ResponseEntity.ok().body(father);
}
问题是当我发送到
http://localhost:8080/api/myapi/newFather
Jsonpost请求:
{
"name": "new father test 12",
"Children": [
{
"name": "Child test 12_1",
},
{
"name": "Child test 12_2",
},
]
}
我得到一个循环作为响应:
{
"id":197,
"name":"father test 12",
"children":[
{
"id":196,
"name":"row test 12_1",
"father":{
"id":197,
"name":"father test 12",
"children":[
{
"id":196,
"name":"row test 12_1",
"father":{ /* ENDLESS LOOP !*/
}
]
}
]
]
只需在Child的Father属性中添加@JsonIgnore注解即可class
@ManyToOne
@JoinColumn(name ="ID_FATHER")
@JsonIgnore
private Father father;
在我的 java-spring 引导应用程序中,我遇到了 2 java class 与注释 @oneToMany 和 @ManyToOne 的 parent-children 双向关系的问题。
当我尝试将它们保存在数据库中并向控制器端点发送 post 请求时,会在两个 object 之间创建一个递归循环。 在controller中调用@getMapping(/{fatherId})方法也出现同样的问题
基本上 java 收到父亲 object 包含一组 Children 引用他们的父亲 object ,其中包含他们的 children榜等等等等..
我该如何解决这个问题?我看到一些类似的 post 建议使用 Jackson 库注释,是否绝对有必要将它添加到 pom 中?我更喜欢在我的 pom 中引用基本的最小包集,而 Jackson 还没有在使用 Spring Initializer(spring boot).
编写的默认包中谢谢大家的建议!
parentclass是父亲:
@Entity
@Table(name="Father")
public class Father {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(mappedBy="father", fetch = FetchType.LAZY)
private Set<Child> children;
一个父亲可以有多个child人,但一个child只能有一个父亲。
childclass:
@Entity
@Table(name="Child")
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name ="ID_FATHER")
private Father father;
控制器中的端点是这样的:
@Transactional
@PostFather(path = "/newFather", consumes = "application/json", produces = "application/json")
public ResponseEntity<?> createNewFather(@Valid @RequestBody Father father, BindingResult result) {
Set<Child> childrenSet = new HashSet<Child>();
//ArrayList<Child> newChildren= new ArrayList<Child>();
//newChildren.addAll(father.getChildren());
for (Child c : father.getChildren()) {
Child newChild = new Child();
newChild.setId(c.getId());
newChild.setName(c.getName());
newChild.setFather(father);
fatherService.saveOrUpdateChild(newChild);
childrenSet.add(newChild);
}
father.setChildren(childrenSet);
Father newFather = fatherService.saveOrUpdateFather(father);
return new ResponseEntity<Father>(newFather, HttpStatus.CREATED);
}
@GetFather("/father/{fatherId}")
public ResponseEntity<?> getFatherById(@PathVariable int fatherId) {
Father father = fatherService.findFatherById(fatherId);
for (Child c : father.getChildren()) {// !!workaround to stop the loop!!
c.setFather(null);
}
return ResponseEntity.ok().body(father);
}
问题是当我发送到
http://localhost:8080/api/myapi/newFather
Jsonpost请求:
{
"name": "new father test 12",
"Children": [
{
"name": "Child test 12_1",
},
{
"name": "Child test 12_2",
},
]
}
我得到一个循环作为响应:
{
"id":197,
"name":"father test 12",
"children":[
{
"id":196,
"name":"row test 12_1",
"father":{
"id":197,
"name":"father test 12",
"children":[
{
"id":196,
"name":"row test 12_1",
"father":{ /* ENDLESS LOOP !*/
}
]
}
]
]
只需在Child的Father属性中添加@JsonIgnore注解即可class
@ManyToOne
@JoinColumn(name ="ID_FATHER")
@JsonIgnore
private Father father;