In my code, I have the Author and Book classes with the Many To One relationship, as below:
Author.java
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Type;
@Entity
public class Autor implements Serializable{
@OneToMany(mappedBy = "autor", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Livro> livros;
// outras coisas...
}
Book.java
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Livro implements Serializable{
@ManyToOne
private Autor autor;
// outras coisas...
}
Note: I'm using MYSQL and Hibernate 3.
I can do the basic operations with this code: create, read, update and remove. But I can only delete a Autor
that has no Livro
related to it. When I try to delete a Autor
that has Livros
, I get this error:
Can not delete or update a parent row: a foreign key constraint fails (
livros
.livro
, CONSTRAINTFK4607E763FA6B4EF
FOREIGN KEY (autor_id
) REFERENCESautor
I would like that when I erase an Author that already has related Books, these would also be removed from the database. I thought the goal of id
was this, but I was not successful using it. Where am I going wrong?
Thank you.
EDIT 1
Looking for more on the internet about this problem, I came across this site .
There it is said that you can not mix JPA annotations with Hibernate (just what I was doing).
Replaced CascadeType.ALL
with @OneToMany([...], cascade = CascadeType.ALL)
, but to no avail. Below the modified class and delete method of my class @Cascade(CascadeType.ALL)
.
Author.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;
@Entity
public class Autor implements Serializable{
@OneToMany(mappedBy = "autor", fetch = FetchType.EAGER)
@Cascade({CascadeType.ALL})
private List<Livro> livros;
// outras coisas...
}
Dao.java
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Dao <Classe>{
public void delete(Classe c){
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.delete(c);
transaction.commit();
session.close();
}
// Outras coisas...
}
EDIT 2
In response, Mark said that you should not mix JPA with Hibernate, but that's how I was doing it from the beginning (and it did not work). I looked for examples on the internet, and my class is basically the same as all the others; the only difference is that I can not make it work. Is it possible that this is a Hibernate version problem? Use o 3.5.6.
EDIT 3
As Marcos Sousa asked, here is my complete project on Github: link