Spring Boot 1.2.5 Hibernate JPA嵌套了很多对很多关系
我有3个表如下
产品{id,title}
Product_image {product_id,image_type_id,path,alt_text}
Product_image_type {id,type_name}
我想要的结果对象是这样的:
Product{ id, title, image_types[ { type_id, type_name, image [ { alt_text, path }, { alt_text, path } ] }, { type_id, type_name, image [ { alt_text, path } ] } ] }
我做了以下
产品类别:
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) @JoinTable( name = "product_image", joinColumns = { @JoinColumn(name = "productId", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "typeId", referencedColumnName = "id") } ) private Set imageTypes = new HashSet();
Product_type_image类:
@ManyToMany(mappedBy="imageTypes") private Set products; @OneToMany(fetch=FetchType.LAZY, mappedBy = "productImageType") private Set productImages = new HashSet();
Product_image类:
@OneToOne(fetch=FetchType.LAZY, mappedBy="productImage") @JoinColumn(name="typeId", referencedColumnName = "id", insertable=false, updatable=false) private ProductImageType oneProductImageType;
我有一种像我想要的格式,但是我得到了一种类型的所有产品图像,我只想获得当前产品的产品图像。 例如我问product_id = 1,我有2种类型,我希望这两种类型中的图像仅用于product_id = 1。
好例子:
Prodcut{ id: 1, title: "product title 1", image_types:[ { type_id: 1 type_name: "type example 1" image [ { product_id: 1, alt_text: "something", path: "http:something", }, { product_id: 1, alt_text: "something2", path: "http:something2", } ] }, { type_id: 2 type_name: "type example 2" image [ { product_id: 1, alt_text: "type 2 something", path: "http:something", }, { product_id: 1, alt_text: "type 2something2", path: "http:something2", } ] } ] }
我现在得到的:
Prodcut{ id: 1, title: "product title 1", image_types:[ { type_id: 1 type_name: "type example 1" image [ { product_id: 1, alt_text: "something", path: "http:something", }, { product_id: 1, alt_text: "something2", path: "http:something2", }, { product_id: 2, alt_text: "something2", path: "http:something2", }, { product_id: 3, alt_text: "something2", path: "http:something2", }, { product_id: 4, alt_text: "something2", path: "http:something2", }, { product_id: 4, alt_text: "something2", path: "http:something2", } ] }, { type_id: 2 type_name: "type example 2" image [ { product_id: 1, alt_text: "type 2 something", path: "http:something", }, { product_id: 1, alt_text: "type 2something2", path: "http:something2", }, { product_id: 1, alt_text: "something2", path: "http:something2", }, { product_id: 2, alt_text: "something2", path: "http:something2", }, { product_id: 3, alt_text: "something2", path: "http:something2", }, { product_id: 4, alt_text: "something2", path: "http:something2", }, { product_id: 4, alt_text: "something2", path: "http:something2", } ] } ] }
表中的内容:
Product Table: id title 1 product title 1 2 product title 2 3 product title 3 4 product title 4 5 product title 5 Product_image Table: product_id image_type_id path alt_text 1 1 http:something something 1 1 http:something2 something2 1 2 http:something type 2 something 1 2 http:something2 type 2 something2 2 1 http:something2 something2 3 1 http:something2 something2 4 1 http:something2 something2 4 1 http:something2 something2 2 2 http:something2 something2 3 2 http:something2 something2 4 2 http:something2 something2 4 2 http:something2 something2 Product_image_type Table: id type_name 1 type example 1 2 type example 2
提前致谢!