【发布时间】: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