Skip to content

Commit 15b53bf

Browse files
committed
relation between parent and child categories, relation between category and product.
1 parent f5612de commit 15b53bf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/main/java/com/manir/springbootecommercerestapi/resource/Category.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import lombok.NoArgsConstructor;
66

77
import javax.persistence.*;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Set;
811

912
@AllArgsConstructor
1013
@NoArgsConstructor
@@ -24,4 +27,15 @@ public class Category {
2427
private String description;
2528
@Column(name = "status")
2629
private String status;
30+
31+
//relation between parent and child categories
32+
@ManyToOne
33+
@JoinColumn(name = "parent_id")
34+
private Category parent;
35+
@OneToMany(mappedBy = "parent")
36+
private Set<Category> children = new HashSet<>();
37+
38+
//relation between category and product
39+
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
40+
private Set<Product> products = new HashSet<>();
2741
}

src/main/java/com/manir/springbootecommercerestapi/resource/Product.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public class Product {
2929
private Integer quantity;
3030
@Column(name = "status")
3131
private String status;
32+
33+
//relation between category and product
34+
@ManyToOne(fetch = FetchType.LAZY)
35+
@JoinColumn(name = "category_id")
36+
Category category;
3237
}

0 commit comments

Comments
 (0)