使用休眠和@JsonIgnore来获取oneToMany和manyToOne [英] fetch oneToMany and manyToOne using hibernate and @JsonIgnore

查看:184
本文介绍了使用休眠和@JsonIgnore来获取oneToMany和manyToOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表A与表B在oneToMany关系中相关 (意味着B在manyToOne中相关),两个关系都设置为EAGER 我看到了,除非我添加以下注释

@JsonIgnore

在manyToOne上(意味着在表B上)我收到一个错误(无限循环) 我从表C到表B的什么是为了从表A中获取数据,它是空的(由于此注释) 我的问题是:如何从表B的表A中获取信息(也许@JsonIgnore是错误的?) 我的休眠版本是5.1.7

解决方案

@JsonIgnore说:不需要将此字段显示在JSON中. Jackson库不会尝试填充此字段.

但是,一旦删除@JsonIgnore,它将导致Jackson进行无数次调用,以获取A的B数据,然后获取B的A数据,此调用将进入Hibernate,并且会出现错误.

您可以

  1. 重新考虑您的架构
  2. 使用DTO对象
  3. 构建自定义查询,然后构建响应

我会先检查选项2,然后可能要检查1.

I have table A that is related to table B in oneToMany relation (meaning B is related in manyToOne) both of the relations are set to EAGER I saw that unless i put the following annotation

@JsonIgnore

on the manyToOne (meaning on table B ) I get an error (infinite loop ) what I am coming from table C to table B in order to get the data from table A it is coming empty (because of this annotation ) my question is : how do I get the information from table A coming table B (maybe the @JsonIgnore is wrong ? ) my hibernate version is 5.1.7

解决方案

@JsonIgnore says: no need this field to be shown in JSON. The Jackson library will not try to populate this field.

But once you remove @JsonIgnore it will lead Jackson to endless number of calls to get B data for A and then A data for B, this calls comes to Hibernate and you getting an error.

You can

  1. rethink your schema
  2. use DTO objects
  3. Build custom queries and then build response

I would check option 2 and probably 1 first.

这篇关于使用休眠和@JsonIgnore来获取oneToMany和manyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆