Usuniecie zamówienia klienta Spring

0

W bazie OrderItem znajduje się pole z zwiazane z tabela CustomerOrder mianowicie
jest to customerOrder_id.

Jesli chce usunąć najpierw tablice orderItem ,aby potem moc usunąć zamowienie Klienta to wyświetla mi się błąd .

Request processing failed; nested exception is java.lang.IllegalArgumentException: attempt to create delete event with null entity.

Wyswietla się ten błąd w usuwaniu tego OrderItem.

package app.Spring.domain;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "CustomerOrder")
public class CustomerOrder {

        @Id
        @Column(name = "orderID")
        private long orderID;

        @ManyToOne(cascade = { CascadeType.MERGE })
        private Register register;

        @Column(name = "status")
        private String status;

        @Column(name = "totalCost")
        private float totalCost;

        @Column(name = "orderDate")
        private Date orderDate;

        public CustomerOrder() {
        }

        public CustomerOrder(long orderID, Register register, String status,
                        float totalCost, Date orderDate) {
                this.orderID = orderID;
                this.register = register;
                this.status = status;
                this.totalCost = totalCost;
                this.orderDate = orderDate;
        }

        public long getOrderID() {
                return orderID;
        }

        public void setOrderID(long orderID) {
                this.orderID = orderID;
        }

        public Register getRegister() {
                return register;
        }

        public void setRegister(Register register) {
                this.register = register;
        }

        public String getStatus() {
                return status;
        }

        public void setStatus(String status) {
                this.status = status;
        }

        public float getTotalCost() {
                return totalCost;
        }

        public void setTotalCost(float totalCost) {
                this.totalCost = totalCost;
        }

        public Date getOrderDate() {
                return orderDate;
        }

        public void setOrderDate(Date orderDate) {
                this.orderDate = orderDate;
        }
}
package app.Spring.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "OrderItem")
public class OrderItem {

        @Id
        @Column(name = "itemID")
        private long itemID;

        @ManyToOne(cascade = { CascadeType.MERGE })
        private Product product;

        @ManyToOne(cascade = { CascadeType.MERGE })
        private CustomerOrder customerOrder;

        @Column(name = "quantity")
        private int quantity;

        public OrderItem() {
        }

        public OrderItem(long itemID, Product product, CustomerOrder customerOrder,
                        int quantity) {
                this.itemID = itemID;
                this.product = product;
                this.customerOrder = customerOrder;
                this.quantity = quantity;
        }

        public long getItemID() {
                return itemID;
        }

        public void setItemID(long itemID) {
                this.itemID = itemID;
        }

        public Product getProduct() {
                return product;
        }

        public void setProduct(Product product) {
                this.product = product;
        }

        public CustomerOrder getCustomerOrder() {
                return customerOrder;
        }

        public void setCustomerOrder(CustomerOrder customerOrder) {
                this.customerOrder = customerOrder;
        }

        public long getQuantity() {
                return quantity;
        }

        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }
}

@Controller
@RequestMapping("/admin/users")
public class AdminController {

    @Autowired
    LocalValidatorFactoryBean validator;

    @Autowired
    RegisterService reg;

    @Autowired  
CustomerOrderService co;
    @Autowired
OrderItemService cos;    

 @RequestMapping("/removecustomer/{id}")
    public String removeCustomerOrderById(@PathVariable Long id) {

        System.out.println("usuwanie  zamowienia o id"+id);
      cos.deleteOrderItem(cos.getOrderItem(id));
    co.deleteCustomerOrder(co.getCustomerOrder(id));

        return "redirect:/";
    }
}

public OrderItem getOrderItem(Long id) {
        return (OrderItem)sessionFactory.getCurrentSession().get(OrderItem.class.getName(), id);
    }

public class CustomerOrderImpl implements CustomerOrderService{

    protected final Logger log = LoggerFactory.getLogger(getClass());

    protected SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }   

  @Override
    public CustomerOrder getCustomerOrder(Long id) {
        return (CustomerOrder)sessionFactory.getCurrentSession().get(CustomerOrder.class.getName(), id);
    }

 public void deleteCustomerOrder(CustomerOrder v) {
            sessionFactory.getCurrentSession().delete(v);
            sessionFactory.getCurrentSession().flush();
        }

}
public class OrderItemImpl implements OrderItemService {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    protected SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }   

    public OrderItem getOrderItem(Long id) {
        return (OrderItem)sessionFactory.getCurrentSession().get(OrderItem.class.getName(), id);

    }
public void deleteOrderItem(OrderItem v) {
            sessionFactory.getCurrentSession().delete(v);
            sessionFactory.getCurrentSession().flush();
        }
}
0
 @RequestMapping("/removecustomer/{id}")
    public String removeCustomerOrderById(@PathVariable Long id) {

        System.out.println("usuwanie  zamowienia o id"+id);
      cos.deleteOrderItem(cos.getOrderItem(id));
    co.deleteCustomerOrder(co.getCustomerOrder(id));

        return "redirect:/";
    }

Wywala w tej metodzie? Wywala błąd bo próbujesz usunąć nulla, metoda get nie znajduje OrderItem o takim id. Na pewno jako argument przekazujesz dobre id? Czy na pewno OrderItem i CustromerOrder mają identyczne id?

0

hmm zrobilem petle po OrderItemach znalazlem wszystkie od tego id customera i dopiero poszlo. Musialem jeszcze metode usunięcia zmienić bo krzyczal Eclipse.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0