7
In my code, I have the classes Author and Book, with the relationship Many To One, as below:
Java author.
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...
}
Java book.
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 am using MYSQL and Hibernate 3.
I can do basic operations with this code: create, read, update and remove. But I can only delete one Autor
that has no Livro
related to it. When I try to delete a Autor
that has Livros
, get that mistake:
Cannot delete or update a Parent Row: a Foreign key Constraint fails (
livros
.livro
, CONSTRAINTFK4607E763FA6B4EF
FOREIGN KEY (autor_id
) REFERENCESautor
(id
))
I would like that when I delete an Author who already has Related Books, they also be removed from the database. I thought the purpose of CascadeType.ALL
was that, but I did not succeed using it. Where I am missing?
Thank you.
EDIT 1
Looking a little more on the internet about this problem, I came across this site.
There it is said that one cannot mix JPA annotations with Hibernate (just what I was doing).
I replaced @OneToMany([...], cascade = CascadeType.ALL)
for @Cascade(CascadeType.ALL)
, but without success. Below the modified class and the delete method of my class Dao
.
Java author.
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, Marcos said that you shouldn’t mix JPA with Hibernate, but that’s how I was doing it from the beginning (and it didn’t work). I looked for examples on the Internet, and my class is basically the same as all the others; the only difference is I can’t make it work. Is it possible that this is a Hibernate version problem? I use 3.5.6.
EDIT 3
As Marcos Sousa asked, here is my complete project on Github: https://github.com/mateusbandeiraa/Livros
Thanks for the answer! 1. I think you reversed the titles AUTHOR & BOOK in his reply: Book has a list
livros
and Author has a listautores
? 2. Thanks for the touch in the relationship, but it’s a simple project and I’m testing all the relationships. Many To Many has been implemented in other classes of the same project. 3. I did the test the way you sent: Book has a list of Authors & Author has a list of Books. I used theCascade = CascadeType.ALL
from JPA, but I still get the bug. Could this be version problem? I am using HIBERNATE 3.5.6– Mateus Bandeira
@Mateusflag thanks for the feed, I’m in the same line of research so we’re in the same boat. I really made a mistake in declaring variables, the code is already adjusted. vc could also, if necessary, send your configuration environment.
– Marcos Sousa
I don’t have much idea what might interfere to cause this error, but I am using Tomcat 8 and Hibernate 3.5.6-final as a dependency on Maven (
hibernate-entitymanager
). I put theCascade
in both classes, but the error persists. In the same project, I copied and glued its classes in full. The error persists. I believe some dependency is missing or the version I use is having some problem. I keep searching...– Mateus Bandeira
@Does Mateusflag still show the same error? how are your delete methods? post your persistence.xml and POM.xml file. how you are controlling your application’s transactions?
– Marcos Sousa
@Mateusbandeira, you can share your complete project, for me to analyze?
– Marcos Sousa
I did the test with Hibernate 4.1.1 and the problem continues. So it would hardly be a version problem... Regarding sharing the code, there is no problem; it is on Github: https://github.com/mateusbandeiraa/Livros
– Mateus Bandeira
@Mateusflag, every question is the exclusion method! vc will have to "break" the relation that the entities have and from there delete the author.
– Marcos Sousa
@Mateusflag, you must take the logic of the answer and adapt to your code.
– Marcos Sousa
You’re talking about rescuing all the author’s books, and erasing them before, right? I’ve thought about it. But wouldn’t that be a scam? I’ll end up doing it anyway.
– Mateus Bandeira