弹簧靴休息为一对多的关系

时间:2018-02-12 05:11:59

标签: java spring-mvc spring-boot spring-data-jpa spring-rest

我创建了春季启动项目,我正在进行休息应用。我使用了My SQL数据库,我正在使用spring数据。有一种方法可以根据客户ID添加订单。所以我无法弄清楚它是否可以基于弹簧数据查询或自定义查询以及它将如何工作? 我只附加了所需的代码,

Customer.java

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "customers")
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name = "cust_ID_PK")
@GeneratedValue(strategy = GenerationType.AUTO)
private int custIDPK;

@Column(name = "billing_city")
private String billingCity;

@Column(name = "billing_country")
private String billingCountry;

@Column(name = "billing_state")
private String billingState;

@Column(name = "billing_street")
private String billingStreet;

@Column(name = "billing_zip")
private String billingZip;

private String company;

@Column(name = "display_name")
private String displayName;

private String email;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "middle_name")
private String middleName;

@Column(name = "other_details")
private String otherDetails;

@Column(name = "print_on_check_as")
private String printOnCheckAs;

@Column(name = "shipping_city")
private String shippingCity;

@Column(name = "shipping_country")
private String shippingCountry;

@Column(name = "shipping_state")
private String shippingState;

@Column(name = "shipping_street")
private String shippingStreet;

@Column(name = "shipping_zip")
private String shippingZip;

private String suffix;

private String title;

// bi-directional many-to-one association to Order
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<Order> orders;

public Customer() {
}

public int getCustIDPK() {
    return this.custIDPK;
}

public void setCustIDPK(int cust_ID_PK) {
    this.custIDPK = cust_ID_PK;
}

public String getBillingCity() {
    return this.billingCity;
}

public void setBillingCity(String billingCity) {
    this.billingCity = billingCity;
}

public String getBillingCountry() {
    return this.billingCountry;
}

public void setBillingCountry(String billingCountry) {
    this.billingCountry = billingCountry;
}

public String getBillingState() {
    return this.billingState;
}

public void setBillingState(String billingState) {
    this.billingState = billingState;
}

public String getBillingStreet() {
    return this.billingStreet;
}

public void setBillingStreet(String billingStreet) {
    this.billingStreet = billingStreet;
}

public String getBillingZip() {
    return this.billingZip;
}

public void setBillingZip(String billingZip) {
    this.billingZip = billingZip;
}

public String getCompany() {
    return this.company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getDisplayName() {
    return this.displayName;
}

public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getFirstName() {
    return this.firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return this.lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getMiddleName() {
    return this.middleName;
}

public void setMiddleName(String middleName) {
    this.middleName = middleName;
}

public String getOtherDetails() {
    return this.otherDetails;
}

public void setOtherDetails(String otherDetails) {
    this.otherDetails = otherDetails;
}

public String getPrintOnCheckAs() {
    return this.printOnCheckAs;
}

public void setPrintOnCheckAs(String printOnCheckAs) {
    this.printOnCheckAs = printOnCheckAs;
}

public String getShippingCity() {
    return this.shippingCity;
}

public void setShippingCity(String shippingCity) {
    this.shippingCity = shippingCity;
}

public String getShippingCountry() {
    return this.shippingCountry;
}

public void setShippingCountry(String shippingCountry) {
    this.shippingCountry = shippingCountry;
}

public String getShippingState() {
    return this.shippingState;
}

public void setShippingState(String shippingState) {
    this.shippingState = shippingState;
}

public String getShippingStreet() {
    return this.shippingStreet;
}

public void setShippingStreet(String shippingStreet) {
    this.shippingStreet = shippingStreet;
}

public String getShippingZip() {
    return this.shippingZip;
}

public void setShippingZip(String shippingZip) {
    this.shippingZip = shippingZip;
}

public String getSuffix() {
    return this.suffix;
}

public void setSuffix(String suffix) {
    this.suffix = suffix;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public List<Order> getOrders() {
    return this.orders;
}

public void setOrders(List<Order> orders) {
    this.orders = orders;
}

public Order addOrder(Order order) {
    getOrders().add(order);
    order.setCustomer(this);

    return order;
}

public Order removeOrder(Order order) {
    getOrders().remove(order);
    order.setCustomer(null);

    return order;
}

}

Order.java

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.List;


@Entity
@Table(name = "orders")
@NamedQuery(name = "Order.findAll", query = "SELECT o FROM Order o")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name = "order_ID_PK")
@GeneratedValue(strategy = GenerationType.AUTO)
private int order_ID_PK;

@Column(name = "custom_message")
private String customMessage;

@Temporal(TemporalType.DATE)
@Column(name = "delivery_due_date")
private Date deliveryDueDate;

@Temporal(TemporalType.DATE)
@Column(name = "invoice_creation_date")
private Date invoiceCreationDate;

@Temporal(TemporalType.DATE)
@Column(name = "payment_due_date")
private Date paymentDueDate;

// bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name = "cust_ID_FK")
private Customer customer;

// bi-directional many-to-many association to Product
@ManyToMany(mappedBy = "orders")
private List<Product> products;

public Order() {
}

public int getOrder_ID_PK() {
    return this.order_ID_PK;
}

public void setOrder_ID_PK(int order_ID_PK) {
    this.order_ID_PK = order_ID_PK;
}

public String getCustomMessage() {
    return this.customMessage;
}

public void setCustomMessage(String customMessage) {
    this.customMessage = customMessage;
}

public Date getDeliveryDueDate() {
    return this.deliveryDueDate;
}

public void setDeliveryDueDate(Date deliveryDueDate) {
    this.deliveryDueDate = deliveryDueDate;
}

public Date getInvoiceCreationDate() {
    return this.invoiceCreationDate;
}

public void setInvoiceCreationDate(Date invoiceCreationDate) {
    this.invoiceCreationDate = invoiceCreationDate;
}

public Date getPaymentDueDate() {
    return this.paymentDueDate;
}

public void setPaymentDueDate(Date paymentDueDate) {
    this.paymentDueDate = paymentDueDate;
}

public Customer getCustomer() {
    return this.customer;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

public List<Product> getProducts() {
    return this.products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}

}

OrderOperation.java

package com.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.model.Order;

public interface OrderOperation extends JpaRepository<Order, Long> {

@Query("SELECT c.orders FROM Customer c where c.custIDPK = :id")
public List<Order> findOrderbyID(@Param("id") int id);
}

CustomerController.java

@RestController
@RequestMapping("/customer")
public class CustomerController {

@Autowired
ICutomerService customerDAO;

@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(value = { "/", "" }, method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<?> getAllCustomer() {
    return new ResponseEntity(customerDAO.getAllCustomer(), HttpStatus.ACCEPTED);
}

@RequestMapping(value = "/{CustomerById}", method = RequestMethod.GET, produces = { "application/json" })
public Customer getCustomerbyId(@PathVariable("CustomerById") String cid) {
    return customerDAO.findCustomerById(Integer.parseInt(cid));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(value = "{CustomerById}/order", method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<?> getAllOrder(@PathVariable("CustomerById") String cid) {
    return new ResponseEntity(customerDAO.getOrdersbyId(Integer.parseInt(cid)), HttpStatus.ACCEPTED);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(value = "order/{CustomerById}/product", method = RequestMethod.GET, produces = {
        "application/json" })
public ResponseEntity<?> getAllProduct(@PathVariable("CustomerById") String cid) {
    return new ResponseEntity(customerDAO.getProductsById(Integer.parseInt(cid)), HttpStatus.ACCEPTED);
}

@SuppressWarnings("rawtypes")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResponseEntity<?> addCustomer(@RequestBody Customer c) {
    boolean flag = customerDAO.addCustomer(c);
    if (flag)
        return new ResponseEntity(HttpStatus.CREATED);
    else
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
}

@SuppressWarnings("rawtypes")
@RequestMapping(value = "/{CustomerById}/orders", method = RequestMethod.POST)
public ResponseEntity<?> addOrders(@PathVariable("CustomerById") String cid, @RequestBody Order c) {
    // c.getCustomer().setCustIDPK(Integer.parseInt(cid));
    boolean flag = customerDAO.addOrder(c);
    if (flag) {
        return new ResponseEntity(HttpStatus.CREATED);
    } else {
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
}
}

我应该如何设计这个addOrders方法?

2 个答案:

答案 0 :(得分:0)

如果您使用的是Spring Data,则需要为要访问的每个表创建一个CrudRepository。 CrudRepository允许您使用标准ORM实践轻松操作表中的数据。 Here is a link了解更多详情。

有关如何使用Spring Data的更多详细信息,请查看this wonderful guide。在使用Spring Data时,本指南已变得不可或缺。

答案 1 :(得分:0)

有很多选择,但我使用了以下方法,所以希望它能帮到你。 @Query注释允许通过将nativeQuery标志设置为true来执行本机查询。

2000-01-01 05:54:42

请根据您的要求编写SQL。