当前位置:编程学习 > JAVA >>

建立外键以后无法插入数据

我建立了下面三个表,其中node对应多个bundle和asset,而bundle对应多个asset,我用的是hibernate,
ModelNode node = new ModelNode();
node.setHierarchy("root-1");
node.setNodeId("node");
node.setType("folder");

ModelBundle bundle = new ModelBundle();
bundle.setId("bundle");
bundle.setParentHUID("node");
bundle.setTimestamp("2012-09-09");

ModelAsset asset = new ModelAsset();
asset.setId("asset");
asset.setParentHUID("bundle");
asset.setTimestamp("2012-09-09");

bundle.getAssets().add(asset);
node.getBundles().add(bundle);
thePersistenceManager.saveOrUpdate(node);

执行上面例子插入数据时,总是提示
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`svod`.`asset`, CONSTRAINT `asset_ibfk_2` FOREIGN KEY (`parentHUID`) REFERENCES `bundle` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2016)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1452)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 17 more

这是为什么啊

CREATE TABLE `node` (
  `hierarchy` varchar(20) NOT NULL,
  `nodeId` varchar(255) NOT NULL,
  `position` int(1) DEFAULT NULL,
   PRIMARY KEY (`hierarchy`),
   INDEX nodeIndex (`nodeId`))

CREATE TABLE `bundle` (
  `id` varchar(255) NOT NULL,
  `parentHUID` varchar(255) NOT NULL,
  `isBlocked` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  foreign key(parentHUID) references node(nodeId) on delete cascade on update cascade


CREATE TABLE `asset` (
  `id` varchar(255) NOT NULL,
  `parentHUID` varchar(255) NOT NULL,
  `isBlocked` tinyint(1) DEFAULT NULL,
   PRIMARY KEY (`id`),
   foreign key(parentHUID) references node(nodeId) on delete cascade on update cascade,
   foreign key(parentHUID) references bundle(id) on delete cascade on update cascade
) --------------------编程问答-------------------- 对应的 (`parentHUID`) 值在`bundle` (`id`) 中不存在 --------------------编程问答-------------------- 什么意思,能说详细点吗? --------------------编程问答-------------------- 已经提示:
a foreign key constraint fails (`svod`.`asset`, CONSTRAINT `asset_ibfk_2` FOREIGN KEY (`parentHUID`) REFERENCES `bundle` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
检查2表的记录`bundle` (`id`)、`asset`(`parentHUID`) --------------------编程问答-------------------- 我知道提示,可是我检查了有两天了,没有问题啊 --------------------编程问答-------------------- 你插入的时候,是先从NODE表插入的? --------------------编程问答-------------------- 是通过hibernate批量插入方式插入的,不知道他会先插入那个表
--------------------编程问答-------------------- 刚才我看了下mysql log,是先插入node的 --------------------编程问答-------------------- 我觉得现在的问题是asset既和bundle有外键关系也和node有外键关系,如果asset的parentHUID和bundle的id对应了,那插入时就提示asset与node之间的外键关系失败,如果asset的parentHUID和node的nodeId对应了,就会提示和bundle的外键关系失败,不知道为什么,大家知道怎么才能实现一个表和两个表之间有外键关系的对应怎么建表吗?我觉得是我建表时外键关系建立可能有问题,可我就是找不到有什么问题
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,