英文:
Spring boot - no back reference property found from type[]
问题
我在Spring中有两个实体:Category(类别)和Position(职位),它们之间有OneToMany(一对多)的关系。
Category.java:
@Entity
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
int parentId;
String name;
@OneToMany(mappedBy = "category")
@JsonManagedReference(value = "referenceCallInCategory")
List<Position> positionList;
// getters and setters
}
Position.java:
@Entity
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
String position;
String company;
String description;
@ManyToOne
@JsonIgnoreProperties({"parentId", "name", "positionList"})
Category category;
// getters and setters
}
当我通过Postman进行职位的POST请求时,在终端中会出现以下警告:
WARN 15904 --- [nio-8080-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class positionsSite.positions.model.Position]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'referenceCallInCategory': no back reference property found from type [collection type; class java.util.List, contains [simple type, class positionsSite.positions.model.Position]]
奇怪的是,当我为类别进行POST请求时,完全相同的警告也会发生,但是在我进行类别的POST请求后,职位的POST请求就可以正常工作,直到我重新启动应用程序,然后问题又出现了。
我之所以使用@JsonIgnoreProperties而不是@JsonBackReference,是因为在进行职位的GET请求时我需要Category.id。考虑到警告中的“no back reference property found”,我尝试了代码中的注释部分@JsonBackReference,但这没有改变任何事情。
非常感谢您对如何解决这个问题的任何帮助。
英文:
I have 2 entities in spring: Category and Position with a OneToMany relation:
Category.java:
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
int parentId;
String name;
@OneToMany(mappedBy = "category")
@JsonManagedReference(value = "referenceCallInCategory")
List<Position> positionList;
// getters and setters
Position.java:
@Entity
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
String position;
String company;
String description;
@ManyToOne
// @JsonBackReference (value = "referenceCallInPosition")
@JsonIgnoreProperties({"parentId","name","positionList"})
Category category;
// getters and setters
When I make a post request for a position through postman I get the following warning in my terminal: WARN 15904 --- [nio-8080-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class positionsSite.positions.mode
l.Position]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'referenceCallInCategory': no back reference property found from type [collection type;
class java.util.List, contains [simple type, class positionsSite.positions.model.Position]]
The strange thing is when making a postrequest for a category the exact same warning occurs but after I make a postrequest for category, the postrequests for position work without a problem. Until I restart the application, then it fails again.
The reason I have @JsonIgnoreProperties instead of @JsonBackReference is that I need the Category.id when I make a get request for position. Given the warning no back reference property found I tried the @JsonBackReference as commented in the code, but this did not change anything.
Any help on how to solve this would be much appreciated
答案1
得分: 0
根据 @darclander 的提及,这篇帖子 清楚地解释了不同的 @Json
属性。我的错误在于我仍然使用了 @JsonManagedReference
,这导致了问题。我将其删除后,现在一切都运行完美。
英文:
As mentioned by @darclander, this post shows clear explanation of the different @Json
properties. My mistake was that I still had a @JsonManagedReference
which gave the problem. I deleted it and not everything works perfect.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论