【问题标题】:Java Jpa @ManyToOne relations missing from JSON resultsJSON 结果中缺少 Java Jpa @ManyToOne 关系
【发布时间】:2018-03-12 22:17:09
【问题描述】:

我正在使用PagingAndSortingRepository 开发 API 我正在尝试从存储库中获取对象列表,并附有它们的关系。这曾经可以工作,但突然停止了这是我的设置;

@RequestMapping(value = "products/imported", produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Iterable<AmazonProduct> importedProducts (...) throws EncoderException, RestClientException {
        ...
        return this.AmazonProductRepository.findAll();
    } 

这是我尝试通过 json 返回的 Product 类;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Entity
@TypeDefs({
    @TypeDef(name = "json", typeClass = MyJsonType.class),
    @TypeDef(name = "featuresJson", typeClass = productFeaturesJsonType.class),
})
public class AmazonProduct {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brandId") 
    @JsonManagedReference
    private AmazonBrand brand;

    public AmazonBrand getBrand() {
        return brand;
    }

    public void setBrand(AmazonBrand brand) {
        this.brand = brand;
    }
}

这是品牌关系;

@Entity
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class AmazonBrand {
    ...

    @JsonBackReference
    @OneToMany(mappedBy="brand", fetch = FetchType.LAZY) 
    private List<AmazonProduct> products;


    public List<AmazonProduct> getProducts() {
        return products;
    }

    public void setProducts(List<AmazonProduct> products) {
        this.products = products;
    }
}

当我通过浏览器运行它时,我得到了;

[ {
  "publisherId" : null,
  "legalDisclaimer" : null,
  "asin" : "B00KSF9RAG",
  "title" : "Eye Mask / Sleep Mask - Sleeping Masks for...",
  "features" : {
    "features" : [ "...", "...", "..." ]
  }
}]

我希望这将与属性一起提供;亚马逊品牌。以前可以用,现在不行了。

我的存储库类如下所示;

public interface AmazonProductRepository extends PagingAndSortingRepository<AmazonProduct, Long> {

}

【问题讨论】:

  • 尝试从 AmazonBrand 属性中删除惰性获取
  • 我试过了,但没有用。
  • 您是否尝试将获取类型更改为渴望?
  • 是的,我改成了FetchType.EAGER
  • 也许你可以尝试明确的'@JsonInclude'

标签: java json spring jpa spring-boot


【解决方案1】:

根据数据库依赖项,您可能需要将注释放在“getters”而不是私有字段上。在 MySql 中使用 Hibernate 时,我遇到了类似的问题。

【讨论】:

  • 我将品牌属性的注释更改为获取 getBrand(); Unable to execute schema management to JDBC target [alter table product add constraint FKti0lsgnnoerhclcve20iho3un foreign key (brand) references brand (id)] 我认为它试图创建 2 个字段。 @Transient 也不起作用。
  • 这可能与@TypeDefs@JsonIgnoreProperties 有关。您是否尝试过使用投影? docs.spring.io/spring-data/rest/docs/current/reference/html/…
【解决方案2】:

尝试在你的实体类上使用@Cacheable(false)

【讨论】:

    【解决方案3】:

    JsonBackReference 的文档说:

    用于指示关联属性是一部分的注释 字段之间的双向链接;并且它的角色是“孩子”(或 “返回”)链接。属性的值类型必须是 bean:不能是 集合、映射、数组或枚举。处理链接使得 使用此注解注解的属性未序列化;和 在反序列化期间,其值设置为具有 “托管”(转发)链接。

    您可以看到您不能将集合与 @JsonBackReference 一起使用,而它必须是一个 bean。

    因此,以下行不起作用:

    @JsonBackReference
    @OneToMany(mappedBy="brand", fetch = FetchType.LAZY) 
    private List<AmazonProduct> products;
    

    我能够获得一些条目,我删除了两个类上的注释。

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多