Spring boot 1.2.5 Hibernate JPA嵌套多对多关系

时间:2015-11-28 15:16:37

标签: java spring hibernate jpa spring-boot

我有3个表,如下面的

产品{     ID,     标题 }

Product_image {     PRODUCT_ID,     image_type_id,     路径,     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<ProductImageType> imageTypes = new HashSet();

Product_type_image类:

@ManyToMany(mappedBy="imageTypes")
private Set<Product> products;

@OneToMany(fetch=FetchType.LAZY, mappedBy = "productImageType")
private Set<ProductImage> 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

提前致谢!

0 个答案:

没有答案