表加外键的限制

展开阅读全文

表主外键问题

04-06

我设置的是本表的主键当本表的外键rnmysql数据库rn建表语句:rnDROP TABLE IF EXISTS `t_news_type`;rnCREATE TABLE `t_news_type` (rn `f_news_type_id` int(8) NOT NULL auto_increment,rn `f_news_type_name` varchar(12) NOT NULL,rn `f_news_type_sub_id` int(8) default NULL,rn PRIMARY KEY (`f_news_type_id`),rn KEY `fk_id` (`f_news_type_sub_id`),rn CONSTRAINT `fk_id` FOREIGN KEY (`f_news_type_sub_id`) REFERENCES `t_news_type` (`f_news_type_id`) ON DELETE CASCADE ON UPDATE CASCADErn) ENGINE=InnoDB DEFAULT CHARSET=utf8;rnrn添加是抛异常:rnDBCExceptionReporter-could not insert: [com.lida.cms.model.NewsType] [insert into t_news_type (f_news_type_name, f_news_type_sub_id) values (?, ?)]rnjava.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails (`cms/t_news_type`, CONSTRAINT `fk_id` FOREIGN KEY (`f_news_type_sub_id`) REFERENCES `t_news_type` (`f_news_type_id`) ON DELETE CASCADE ON UPDATE CASCADE)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)rn at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1125)rn at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:677)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1357)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1274)rn at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1259)rn at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)rn at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)rn at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)rn at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)rn at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)rn at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)rn at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)rn at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)rn at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)rn at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)rn at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)rn at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)rn at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)rn at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)rn at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)rn at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)rn at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)rn at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)rn at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:697)rn at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)rn at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)rn at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)rn at com.lida.cms.daoimpl.UserDaoImpl.addNewsType(UserDaoImpl.java:189)rn at com.lida.cms.service.impl.UserManagerImpl.addNewsType(UserManagerImpl.java:122)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 论坛

没有更多推荐了,返回首页