gpt4 book ai didi

JSON 结果中缺少 Java Jpa @ManyToOne 关系

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:32 25 4
gpt4 key购买 nike

我正在使用PagingAndSortingRepository开发一个API,我试图从存储库中获取对象列表及其所附加的关系。这曾经有效,但突然停止了,这是我的设置;

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

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

@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" : [ "...", "...", "..." ]
}
}]

我希望这能与属性(property)一起提供;亚马逊品牌。以前可以用,但现在不行。

我的存储库类如下所示;

public interface AmazonProductRepository extends PagingAndSortingRepository<AmazonProduct, Long> {

}

最佳答案

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

关于JSON 结果中缺少 Java Jpa @ManyToOne 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46515416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com