0
我試圖堅持一個訂單實體,其中有支付實體列表。保存操作是級聯的,因此保存訂單將保存其中的所有付款。訂單表具有orderid,price,qty和Payment表具有orderid,paymentid,value,創建和更新。在保存訂單時,通過orderid自動生成的訂單可以完美地保存訂單,但對於付款,一切都很好,除了orderid即將發生空。訂單表中的訂單應分配給付款表中的訂單。請幫忙。堅持使用spring crud repository的一對多關係
Order.java
@Entity
@Table(schema="public")
@NamedQuery(name="Order.findAll", query="SELECT o FROM Order o")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int orderid;
private Integer price;
private Integer qty;
//bi-directional many-to-one association to Payment
@OneToMany(mappedBy="order", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<Payment> payments;
public Order() {
}
public Integer getPrice() {
return this.price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getQty() {
return this.qty;
}
public void setQty(Integer qty) {
this.qty = qty;
}
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public List<Payment> getPayments() {
return this.payments;
}
public void setPayments(List<Payment> payments) {
this.payments = payments;
}
public Payment addPayment(Payment payment) {
if(payment!=null){
payment.setOrder(this);
getPayments().add(payment);
}
return payment;
}
public Payment removePayment(Payment payment) {
getPayments().remove(payment);
payment.setOrder(null);
return payment;
}
@Override
public String toString() {
return "Order [orderid=" + orderid + ", price=" + price + ", qty=" + qty + ", payments=" + payments + "]";
}
}
payment.java
@Entity
@Table(schema="public")
@NamedQuery(name="Payment.findAll", query="SELECT p FROM Payment p")
public class Payment implements Serializable {
private static final long serialVersionUID = 1L;
@Temporal(TemporalType.DATE)
private Date created;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer paymentid;
@Temporal(TemporalType.DATE)
private Date updated;
private Integer value;
//bi-directional many-to-one association to Order
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="orderid", referencedColumnName="orderid")
private Order order;
public Payment() {
}
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
public Integer getPaymentid() {
return this.paymentid;
}
public void setPaymentid(Integer paymentid) {
this.paymentid = paymentid;
}
public Date getUpdated() {
return this.updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public Integer getValue() {
return this.value;
}
public void setValue(Integer value) {
this.value = value;
}
public Order getOrder() {
return this.order;
}
public void setOrder(Order order) {
this.order = order;
}
@Override
public String toString() {
return "Payment [created=" + created + ", paymentid=" + paymentid + ", updated=" + updated + ", value=" + value
+ ", order=" + order + "]";
}
}
orderController.java
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@Autowired
PaymentService paymentService;
@RequestMapping(method=RequestMethod.PUT, value="/order")
public void createOrder(@RequestBody Order o){
System.out.println(o);
orderService.createOrUpdateOrder(o);
System.out.println("Order Created");
}
@RequestMapping(method=RequestMethod.POST, value="/order")
public void updateOrder(@RequestBody Order o){
orderService.createOrUpdateOrder(o);
System.out.println("Order Updated");
}
@RequestMapping(method=RequestMethod.DELETE, value="/order")
public void deleteOrder(@RequestBody Order o){
orderService.deleteOrder(o);
System.out.println("Order Deleted");
}
@RequestMapping(method=RequestMethod.PUT, value="/payment")
public void createPayment(@RequestBody Order o){
paymentService.createOrUpdatePayments(o);
System.out.println("Payment Created");
}
@RequestMapping(method=RequestMethod.POST, value="/payment")
public void updatePayment(@RequestBody Order o){
paymentService.createOrUpdatePayments(o);
System.out.println("Payment Updated");
}
@RequestMapping(method=RequestMethod.DELETE, value="/payment")
public void deletePayment(@RequestBody Order o){
paymentService.deletePayments(o);;
System.out.println("Payment Deleted");
}
}
orderservice.java
@Service
public class OrderService {
@Autowired
OrderRepository orderRepository;
public void createOrUpdateOrder(Order o){
orderRepository.save(o);
}
public void deleteOrder(Order o){
orderRepository.delete(o.getOrderid());
}
public Order getOrderDetails(Order o){
return orderRepository.findOne(o.getOrderid());
}
}
OrderRepository.java
@Repository
public interface OrderRepository extends CrudRepository<Order, Integer> {
}
輸出:
Payment table where orderid is null
你能分享你如何儲蓄? –
我已經在後期添加了。 –
迭代'Payment'列表並執行'setOrder(order);'到每個'payment' –