one to many relation persisting using spring crud repository

时间:2017-08-05 10:38:24

标签: hibernate spring-mvc spring-boot annotations spring-data-jpa

I am trying to persist one order entity which has list of payment entity inside it. The save operation is cascaded such that saving order will save all the payments inside it. Order table has orderid, price, qty and Payment table has orderid, paymentid, value, created and updated. When saving the order, the order is getting saved perfectly with orderid auto generated but for payment all is fine except orderid is coming a null. The orderid from order table should be assigned to the orderid in payment table. Please help.

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> {
}

output:

Order Table

Payment table where orderid is null

1 个答案:

答案 0 :(得分:0)

You can try this in createOrUpdateOrder of service and let me know
May be you can find better than this

public void createOrUpdateOrder(Order o){
    List<Payment> payments = new ArrayList<>();
    for( Payment payment : order.getPayments() ) {
        payment.setOrder( order );
        payments.add(payment);
    }
    order.setPayments( payments );
    orderRepository.save(o);
}