java – Hibernate:外键约束违规问题

前端之家收集整理的这篇文章主要介绍了java – Hibernate:外键约束违规问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的代码中有一个com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException(使用Hibernate和Spring),我无法理解为什么.
我的实体是Corpus和Semspace,在我的hibernate映射配置中定义了从Semspace到Corpus的多对一关系:

生成异常的Java代码是:

Corpus corpus = Spring.getCorpusDAO().getCorpusById(corpusId);
Semspace semspace = new Semspace();
semspace.setCorpus(corpus);
semspace.setName(name);
Spring.getSemspaceDAO().save(semspace);

我检查了并且语料库变量不为空(因此它在数据库中使用DAO检索)
完整的例外是:

com.MysqL.jdbc.exceptions.MysqLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`xxx/Semspace`,CONSTRAINT `FK4D6019AB6556109` FOREIGN KEY (`idCorpus`) REFERENCES `Corpus` (`idCorpus`))
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:931)
at com.MysqL.jdbc.MysqLIO.checkErrorPacket(MysqLIO.java:2941)
at com.MysqL.jdbc.MysqLIO.sendCommand(MysqLIO.java:1623)
at com.MysqL.jdbc.MysqLIO.sqlQueryDirect(MysqLIO.java:1715)
at com.MysqL.jdbc.Connection.execsql(Connection.java:3249)
at com.MysqL.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:642)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:639)
at xxx.dao.impl.AbstractDAO.save(AbstractDAO.java:26)
at org.apache.jsp.functions.semspaceManagement_jsp._jspService(semspaceManagement_jsp.java:218)
[...]

外键约束已被hibernate创建(并添加数据库),我没有看到可以违反约束的位置.该表是innodb,我试图删除所有表并重新创建它仍然存在问题…

编辑:嗯,我想我有一个开始的答案…我将hibernate的日志级别更改为DEBUG,在崩溃之前我有以下日志

insert into Semspace (name,[...]) values (?,[...])

因此看起来它不会尝试插入idCorpus,因为它不是null,它使用默认值“0”,它不引用Corpus表中的现有条目…

最佳答案
我一直对关联映射感到困惑.查看所需的关联以及用于创建assiocation的hibernate映射配置.

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/associations.html

是一个伟大的协会食谱书.

猜你在找的Spring相关文章