Delete entity and all its relationships Hibernate

Asked

Viewed 717 times

2

I have the entity Fatura who has a relationship OneToMany with the entity Item.

When I try to delete an Invoice I have the following error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.Mysqlintegrityconstraintviolationexception: Cannot delete or update a Parent Row: a Foreign key Constraint fails (bd_afunopi.item, CONSTRAINT FK_26i46vxf33rt1kv9wrwl42t5d FOREIGN KEY (fatura_nota) REFERENCES fatura (nota))

I am setting Calendar. Type as ALL in the two entities, this way:

Invoice:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "fatura", cascade = CascadeType.ALL)
private List<Item> items = new ArrayList<Item>();

Item:

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fatura_nota", nullable = true)
private Fatura fatura;

How can I delete this invoice and all its items?

  • How’s the foreclosure coming? Take a look at this topic here: http://stackoverflow.com/questions/32146911/hibernate-throws-cannot-delete-or-update-a-parent-row-a-foreign-key-constraint Ps: Watch out for the comment explaining the remove: delete(Session.get(Employee.class, 10)); where 10 is the ID he wants to remove.

  • @Antôniojúnior was just that, thank you. Post the answer I will mark it as correct.

  • Oops, thank you too! :)

1 answer

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.