Hibernate version: 3.2.5.ga
Mapping documents: annotations
Code between sessionFactory.openSession() and session.close(): Using HibernateDaoSupport
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySQL 5.0
The generated SQL (show_sql=true): true
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html
Hello. I am new to hibernate.
I have an entity called Company with a link to Audit. When I delete a Company I want all the Audits deleted as well. Here is how Company is defined.
Code:
@Entity
@NamedQueries({
@NamedQuery(
name="CompanyByName",
query="from Company l where l.name = :name")
})
public class Company implements Serializable {
@OneToMany(cascade=CascadeType.ALL)
private List<Audit> audits = new ArrayList<Audit>();
Here is how Audit looks.
Code:
@Entity
@NamedQueries({
@NamedQuery(
name="AuditByCompany",
query="from Audit l where l.company = :company")
})
public class Audit implements Serializable {
private static final long serialVersionUID = -220980587408360814L;
@ManyToOne
private Company company;
I generate the schema using mvn hibernate3:hbm2ddl. I am able to save things fine. However when I try and delete a Company I get the following error:
Code:
//here is the code that deletes, in a method that is part of a class that extends HibernateDaoSupport
public void delete(Company c) {
getHibernateTemplate().delete(c);
}
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`data`.`audit`, CONSTRAINT `FK3CAABBB4C674D5C` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`))
When I check out a table I can see there is a foreign key constraint.
However I thought cascade=CascadeType.ALL would handle this. What am I not understanding?
Thanks,
Luke