Hibernate 带Spring和Jackson的递归性
我需要创建一个带有树实体的模型,所有这些实体之间都有一个引用,我需要使用Jackson的API将其序列化到我的AngularJS应用程序,但是,当我到达端点时,返回的JSON是错误的。有什么办法来做这个?更好的方法还是修复它的方法 地点类别:Hibernate 带Spring和Jackson的递归性,hibernate,rest,recursion,model-view-controller,jackson,Hibernate,Rest,Recursion,Model View Controller,Jackson,我需要创建一个带有树实体的模型,所有这些实体之间都有一个引用,我需要使用Jackson的API将其序列化到我的AngularJS应用程序,但是,当我到达端点时,返回的JSON是错误的。有什么办法来做这个?更好的方法还是修复它的方法 地点类别: @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @Entity(name = "od_places") public cl
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity(name = "od_places")
public class place {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private int number;
@Column
private int zone;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "place_id")
private List<Dweller> dwellers;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id")
private List<Route> routes;
@Column
private Geometry geom;
@Column
private float x;
@Column
private float y;
@Column
private Date lastUpdate;
@Column
private Date regDate;
@Column
private boolean disabled;
}
路线等级:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity(name = "od_routes")
public class Route {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String weekDay;
@Column
private int trip;
@OneToOne(orphanRemoval = true)
private Place origin;
@Column
private String departureTime;
@OneToOne(orphanRemoval = true)
private Place destiny;
@Column
private String arrivalTime;
@OneToOne(orphanRemoval = true)
private Dweller dweller;
@Column
private String reason;
@Column
private String travelMode;
@Column
private Date lastUpdate;
@Column
private Date regDate;
@Column
private boolean disabled;
}
邮递员回应:
[
{
"id": 1,
"name": "Prefeito Mario de Menezes, Avenida",
"number": 1,
"zone": 1,
"dwellers": [
{
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1,
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"routes": [
{
"id": 1,
"weekDay": "Terça-Feira",
"trip": 1,
"origin": 1, // THIS SHOULD BE AN OBJECT!
"departureTime": "08:00 - 08:29",
"destiny": {
"id": 2,
"logDom": "Ronat Valter Sodré, Rua",
"numLogDom": 2,
"ztDom": 2,
"dwellers": [],
"routes": [
{
"id": 2,
"weekDay": "Terça-Feira",
"trip": 2,
"origin": 2,
"departureTime": "08:30 - 08:59",
"destiny": 1,
"arrivalTime": "11:30 - 11:59",
"dweller": {
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1, // THIS SHOULD ALSO BE AN OBJECT!
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
},
"reason": "Retorno à residência",
"travelMode": "Moto (como motorista)",
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"geom": null,
"x": -23.274357,
"y": -51.060287,
"lastUpdate": 1583809200000,
"regDate": 1583809200000,
"disabled": false
},
"arrivalTime": "08:00 - 08:29",
"dweller": {
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1,
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
},
"reason": "Lazer",
"travelMode": "Moto (como motorista)",
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"geom": null,
"x": -23.266556,
"y": -51.038334,
"lastUpdate": 1583809200000,
"regDate": 1583809200000,
"disabled": false
},
2, // THIS NUMBER TWO BREAKS EVERYTHING HERE! WHERE IS MY SECOND OBJECT?
.... // The others object are returned as the first one, just the second object returns as a number 2.
]
}
有几件事需要说明:
我真的需要位置模型列表,因为我希望我装载的每个地方都有他的路线。
我和博士后一起工作。
所有数据都是模拟的
我想要的是:
我想解决这个问题,让它变得更好,并学习为什么以及如何让它起作用。尼古拉·舍甫琴科在评论中给出的答案对我有用
“在您的位置,您与Route和Resident有一对多的关系,而在Route和Resident中,您与Place有一对一的关系(向后)。因此,首先您应该正确地设计您的表。此外,此链接可能对递归Jackson引用有帮助”您是否使用Spring Data JPA?或者整个序列化是如何进行的呢?在你的
地点
中,你与路线
和居住者
有一对多的关系,而在路线
和居住者
中,你与地点
有一对一的关系(向后)。所以首先你应该正确设计你的桌子。这个链接可能对递归Jackson引用很有帮助Sebastiaan,我使用的是Spring数据JPA。通过从我的端点返回ResponseEntity,序列化是自动的。我很确定这件作品是Jasckson做的对吧?哇!尼古拉,你帮了大忙!!我总是和许多人或一个女人搞得一团糟!JsonManagedReference为我工作!谢谢我所做的是修复:修复了模型上的关系,将JsonManagedReference和JsonBackReference命名等等。@NikolaiShevchenko您能否详细说明您的评论,作为对这个问题的回答,以便提问者能够接受?
[
{
"id": 1,
"name": "Prefeito Mario de Menezes, Avenida",
"number": 1,
"zone": 1,
"dwellers": [
{
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1,
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"routes": [
{
"id": 1,
"weekDay": "Terça-Feira",
"trip": 1,
"origin": 1, // THIS SHOULD BE AN OBJECT!
"departureTime": "08:00 - 08:29",
"destiny": {
"id": 2,
"logDom": "Ronat Valter Sodré, Rua",
"numLogDom": 2,
"ztDom": 2,
"dwellers": [],
"routes": [
{
"id": 2,
"weekDay": "Terça-Feira",
"trip": 2,
"origin": 2,
"departureTime": "08:30 - 08:59",
"destiny": 1,
"arrivalTime": "11:30 - 11:59",
"dweller": {
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1, // THIS SHOULD ALSO BE AN OBJECT!
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
},
"reason": "Retorno à residência",
"travelMode": "Moto (como motorista)",
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"geom": null,
"x": -23.274357,
"y": -51.060287,
"lastUpdate": 1583809200000,
"regDate": 1583809200000,
"disabled": false
},
"arrivalTime": "08:00 - 08:29",
"dweller": {
"id": 1,
"gender": "Masculino",
"deficient": false,
"ageRange": "20 Anos",
"schooling": "Graduado",
"ocupation": "Assalariado",
"place": 1,
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
},
"reason": "Lazer",
"travelMode": "Moto (como motorista)",
"lastUpdate": 1583895600000,
"regDate": 1583895600000,
"disabled": false
}
],
"geom": null,
"x": -23.266556,
"y": -51.038334,
"lastUpdate": 1583809200000,
"regDate": 1583809200000,
"disabled": false
},
2, // THIS NUMBER TWO BREAKS EVERYTHING HERE! WHERE IS MY SECOND OBJECT?
.... // The others object are returned as the first one, just the second object returns as a number 2.
]
}