打算删除数据,出现错误
Cannot delete or update a parent row: a foreign key constraint fails (`oa`.`application`, CONSTRAINT `FKC00DAD305FBE03CF` FOREIGN KEY (`applicationTemplateId`) REFERENCES `applicationtemplate` (`id`))
FreeMarker template error!
Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.ConstraintViolationException: could not execute update query
The problematic instruction:
----------
==> ${msg[0]} [on line 68, column 29 in org/apache/struts2/dispatcher/error.ftl]
----------
Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.ConstraintViolationException: could not execute update query
原因是
如果是MySQL,方言一定要是MySQL5InnoDBDialect
注意是MySQL5InnoDBDialect不是MySQLInnoDBDialect,
如何用MySQLInnoDBDialect再使用
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
SessionFactory sf =(SessionFactory) act.getBean("mySessionFactory");无法创建表!
如果之前写的不是MySQL5InnoDBDialect,改完beans.xml中的<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>后
需要删除之前创建的表,然后重新创建
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
SessionFactory sf =(SessionFactory) act.getBean("mySessionFactory");
如果不删除相关表重新创建还是会出现最开始的错误错误!