Delete entity and all its Hibernate relationships

2

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

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

  

Caused by:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   Can not delete or update parent row: a foreign key constraint fails   (% with% of%, CONSTRAINT% with%   FOREIGN KEY ( bd_afunopi ) REFERENCES item ( FK_26i46vxf33rt1kv9wrwl42t5d ))

I'm setting cascade.Type as ALL in the two entities, like this:

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 do I delete this invoice and all your items?

    
asked by anonymous 29.01.2016 / 13:34

1 answer

1

Take a look at this topic here: link

Ps: Watch out for the comment that explains the remove:      delete (session.get (Employee.class, 10));

where 10 is the ID he wants to remove.

    
29.01.2016 / 15:55