0

I tried many methods on the net, but it doesn't work. I want to delete data from a database using hibernate, but I get this errors SEVERE: Cannot delete or update a parent row: a foreign key constraint fails (sakila.comanda, CONSTRAINT comanda_ibfk_1 FOREIGN KEY (IDPRODUS) REFERENCES produs (IDPRODUS)) SEVERE: Could not synchronize database state with session

comanda means order and produs means product

Here is the code:

    private void StergeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    try{
        org.hibernate.Transaction tx = session.beginTransaction();

        int idprodus = ((Produs)IdProdusComboBox.getSelectedItem()).getIdprodus();
        Produs produs = (Produs) session.get(Produs.class, idprodus);

        session.delete(produs);

        tx.commit();
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
}
1
  • You have posted the mapping of "comanda" twice and the mapping of "produs" is missing. Commented May 28, 2012 at 8:53

1 Answer 1

2

This just means that deleting the product is impossible, because it would break a foreign key constraint in database. You have orders for your product, but you're trying to delete the product. How will you honor the orders? You have to decide:

  • either you delete the orders referencing the product before deleting the product
  • or you keep the orders in the database, but these orders must not reference the product anymore.

Not doing any of the above would leave the database in an inconsistent state: orders for products which don't exist. The foreign key constraint in the database ensures that such an inconsistent state is impossible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.