Relation Database

This commit is contained in:
Chantha
2020-05-18 14:51:58 +07:00
parent ba9e100a18
commit 2d8b87cb5b
12 changed files with 123 additions and 33 deletions

View File

@@ -0,0 +1,4 @@
package com.chantha.jdbc.config;
public class WebConfig {
}

View File

@@ -5,6 +5,7 @@ import com.chantha.jdbc.jpa.model.view.ProductWithOrderDetail
import com.chantha.jdbc.jpa.repo.ProductRepo
import com.chantha.jdbc.jpa.service.product.ProductService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@@ -15,6 +16,7 @@ public class ProductController @Autowired constructor(private val productService
fun product():List<ProductWithOrderDetail>{
return productService.fetchAllProducts()
}
@GetMapping("/api/product/getproduct")
fun getAllProduct():List<Product>{
return productRepo.readAllRecord()

View File

@@ -72,4 +72,5 @@ public class Customer implements Serializable {
@JsonManagedReference
@OneToMany(mappedBy = "customer")
private List<Order> orders;
}

View File

@@ -31,6 +31,11 @@ public class Order implements Serializable {
@JoinColumn(name = "cusId",nullable = false,referencedColumnName = "id")
private Customer customer;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "sid",nullable = false,referencedColumnName = "id")
private Staff staff;
@OneToMany(mappedBy = "order")
private List<OrderDetail> orderDetails;
@@ -39,6 +44,14 @@ public class Order implements Serializable {
return orderId;
}
public Staff getStaff() {
return staff;
}
public void setStaff(Staff staff) {
this.staff = staff;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
@@ -80,10 +93,11 @@ public class Order implements Serializable {
public Order() {
}
public Order(Long orderId,Date orderDate,double amount,Customer customer,List<OrderDetail> orderDetails) {
public Order(Long orderId,Date orderDate,double amount,Customer customer,List<OrderDetail> orderDetails,Staff staff) {
this.orderId=orderId;
this.orderDate=orderDate;
this.amount=amount;
this.orderDetails=orderDetails;
this.staff=staff;
}
}

View File

@@ -0,0 +1,25 @@
package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String tittle;
@OneToMany(mappedBy = "position")
@JsonManagedReference
private List<Staff> staff;
}

View File

@@ -0,0 +1,33 @@
package com.chantha.jdbc.jpa.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Staff {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Gender gender;
private double salary;
@JsonManagedReference
@OneToMany(mappedBy = "staff")
private List<Order> orderList;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "posId",nullable = false,referencedColumnName = "id")
private Position position;
}