up1 / assignment-java-boot-camp

8 stars 103 forks source link

ขอตัวอย่างการเขียน select join table #20

Closed petchy closed 2 years ago

petchy commented 2 years ago

ผมเก็บข้อมูล product กับ thumbsnail_images แยกกัน ผมต้องการเขียน join 2 table แล้วได้ response แบบนี้ image

ขอตัวอย่างโค้ดหน่อยครับ ลองทำตามตัวอย่างที่หามาแต่ไม่ได้ครับ

up1 commented 2 years ago

มีใน Spring Data JPA หรือจะใช้ Spring Data JDBC ได้ครับ

หลัก ๆ คือ เรื่องของ relationship ใน Entity class ครับ

ยกตัวอย่างเช่น

@Entity 
class Product {

    //...     

    @OneToMany(mappedBy="cart")
    private List<MyImage> thumbsnail_images;

    //...
}

@Entity
class MyImage {}

ตัวอย่าง code ของ JPA

petchy commented 2 years ago

ขอบคุณครับ