我有这个产品实体:
@Entity
public class Product implements Serializable{
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
@JsonBackReference
private ProductCategory productCategory;
}
和类别实体:
@Entity
public class ProductCategory implements Serializable{
@OneToMany(fetch = FetchType.LAZY, mappedBy="productCategory" ,cascade = CascadeType.ALL)
@JsonManagedReference
@OrderBy("id desc")
private Set<Product> products = new HashSet<Product>();
}
Rest API:
@Query("select p,productCategory.id,productCategory.name from Product p inner join p.productCategory productCategory")
public Page<Product> getProductsWithCategory(Pageable pageable);
返回以下JSON:
"content": [
[
{
"id": 2,
"sku": "REF_25471",
"name": "Serrure Washington"
},
1,
"Hello 1"
],
我想在JSON中添加类别对象,如下所示:
"content": [
{
"id": 1,
"name": "Hello 1",
"products": [
{
"id": 4,
"sku": "REF_25472",
"name": "Serrure Washington",
"productCategory": [
{
"id": 1,
"name": "Hello 1"
}
]
}
]
}
]