首先,它是正确的吗?例如,一个供应商有多个付款,并且供应商有一个身份,即他的信息。
我想要插入供应商支付的付款。 我目前正在发送一个具有以下元素的json文件。
{
"date":"2017-02-17",
"dueDate":"2018-02-17",
"payable":2000,
"paid":1000,
"supplierId":1
}
现在在控制器中,我正在读取请求的JSON,提取供应商的 Id ,然后找到其类的对象,然后将其传递给 Payment 以添加付款。这是正确的方式吗?
case "payment": {
logger.debug("The condition for the insertion of the payment.");
try {
logger.debug("Populating the request Map with the request to identify the type of .");
requestMap = mapper.readValue(requestBody, Map.class);
} catch (IOException e) {
e.printStackTrace();
}
logger.debug("Identifying the key for the payment is it for customer or supplier.");
if ((requestMap.containsKey("customerId")) || requestMap.containsKey("pending")){
logger.debug("The request for the adding payment of the customer has been received.");
Customer customer = customerRepository.findById(Integer.parseInt(requestMap.get("customerId")));
// Payment payment = new Payment(requestMap.get("dueDate"), requestMap.get("pending"), customer, requestMap.get("data"))
// paymentRepository.save()
} else if (requestMap.containsKey("supplierId") || requestMap.containsKey("outstanding")){
}
}
这里有一个信息模型。
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "Information")
public class Information {
private Integer id;
private String name;
private String contactNo;
private String email;
private String address;
private Customer customer;
private Supplier supplier;
public Information() {
}
public Information(String name, String contactNo, String email, String address) {
this.name = name;
this.contactNo = contactNo;
this.email = email;
this.address = address;
}
public Information(Customer customer) {
this.customer = customer;
}
public Information(Supplier supplier) {
this.supplier = supplier;
}
/**
*
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "contact_no", unique = true, nullable = false)
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
@Column(name = "email", unique = true, nullable = false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@OneToOne(mappedBy = "information")
@JsonBackReference(value = "customer-reference")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@OneToOne(mappedBy = "information")
@JsonBackReference(value = "supplier-reference")
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
@Override
public String toString() {
return "Information{" +
"id=" + id +
", name='" + name + '\'' +
", contactNo='" + contactNo + '\'' +
", email='" + email + '\'' +
", address='" + address + '\'' +
'}';
}
}
这里有一个客户,还有一些其他相关的关系,不用理会,我只想了解这个概念。
@Entity
@Table(name = "Customer") //maps the entity with the table. If no @Table is defined,
// the default value is used: the class name of the entity.
public class Customer {
private Integer id;
private Information information;
private Set<Payment> payments;
private Set<Sale> sales;
private Set<Orders> orders;
public Customer() {
}
public Customer(Information information) {
this.information = information;
}
/**
*
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
@JoinColumn(name = "Information_id")
@JsonManagedReference(value = "customer-reference")
public Information getInformation() {
return information;
}
public void setInformation(Information information) {
this.information = information;
}
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
public Set<Payment> getPayments() {
return payments;
}
public void setPayments(Set<Payment> payments) {
this.payments = payments;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CustomerSales", joinColumns = @JoinColumn(name = "Customer_id",
referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Sale_id", referencedColumnName = "id"))
public Set<Sale> getSales() {
return sales;
}
public void setSales(Set<Sale> sales) {
this.sales = sales;
}
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
@Override
public String toString() {
return String.format("Customer{id = %d, " +
"name = %s, " +
"contact_no = %s, " +
"address = %s, " +
"email = %s}",
id,information.getName(), information.getContactNo(), information.getAddress(), information.getEmail());
}
}
这里是供应商模型。
@Entity
@Table(name = "Supplier")
public class Supplier {
private Integer id;
private Information information;
private Set<Payment> payments;
private Set<Orders> orders;
private Set<Purchase> purchases;
public Supplier() {
}
public Supplier(Information information) {
this.information = information;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "InformationId")
@JsonManagedReference(value = "supplier-reference")
public Information getInformation() {
return information;
}
public void setInformation(Information information) {
this.information = information;
}
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
public Set<Payment> getPayments() {
return payments;
}
public void setPayments(Set<Payment> payments) {
this.payments = payments;
}
@OneToMany(mappedBy = "supplier", cascade = CascadeType.ALL)
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "SupplierPurchases", joinColumns = @JoinColumn(name = "Supplier_id",
referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "Purchase_id", referencedColumnName = "id"))
public Set<Purchase> getPurchases() {
return purchases;
}
public void setPurchases(Set<Purchase> purchases) {
this.purchases = purchases;
}
}
最后但同样重要的是我们有支付模型。
@Entity
@Table(name="Payment")
public class Payment {
private Integer id;
private Date dueDate;
private Long paid;// When you are in debit you have to pay to supplier
private Long payable; // When you have to take from customer.
private Date date;
private Customer customer;
private Supplier supplier;
private PaymentMethod paymentMethod;
public Payment() {
}
public Payment(Date dueDate, Long payable, Date date, Customer customer, PaymentMethod paymentMethod) {
this.dueDate = dueDate;
this.paid = payable;
this.date = date;
this.customer = customer;
this.paymentMethod = paymentMethod;
}
public Payment(Date dueDate, Long paid, Date date, Supplier supplier, PaymentMethod paymentMethod) {
this.dueDate = dueDate;
this.paid = paid;
this.date = date;
this.supplier = supplier;
this.paymentMethod = paymentMethod;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false)
public Date getDueDate() {
return dueDate;
}
public void setDueDate(Date dueDate) {
this.dueDate = dueDate;
}
public Long getPaid() {
return paid;
}
public void setPaid(Long paid) {
this.paid = paid;
}
public Long getPayable() {
return payable;
}
public void setPayable(Long payable) {
this.payable = payable;
}
@ManyToOne
@JoinColumn(name = "CustomerId")//mappedBy indicates the entity is the inverse of the relationship.
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@ManyToOne
@JoinColumn(name = "SupplierId")
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@ManyToOne
@JoinColumn(name = "PaymentMethodId")
public PaymentMethod getPaymentMethod() {
return paymentMethod;
}
public void setPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
@Override
public String toString() {
return "Payment{" +
"id=" + id +
", dueDate=" + dueDate +
", paid=" + paid +
", payable=" + payable +
", date=" + date +
'}';
}
还有没有更好的方法来执行这个任务?
感激不尽:)