本文介绍了带有hibernate的外键和mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用Hibernate在mySql数据库中创建表。我有两个表conge和employee,我有很多关系。 conge表有一个员工ID的外键。



这是我的错误



无法添加或更新子行:外键约束失败(concretepage.Conge,CONSTRAINT FKk1p9i6bbic92cgaad05k4smsg FOREIGN KEY(id)REFERENCES EMPLOYE(id))



谢谢,



我已导出数据库这是sql文件

I am using Hibernate to create table in mySql database. I have two table "conge" and "employee" and I have a manytoone relationship. the "conge" table have a foreign key of the employee id.

this is my error

Cannot add or update a child row: a foreign key constraint fails (concretepage.Conge, CONSTRAINT FKk1p9i6bbic92cgaad05k4smsg FOREIGN KEY (id) REFERENCES EMPLOYE (id))

Thanks,

I have exported the database this is the sql file

-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jul 06, 2017 at 11:50 AM
-- Server version: 10.1.24-MariaDB
-- PHP Version: 7.1.6

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `concretepage`
--

-- --------------------------------------------------------

--
-- Table structure for table `Conge`
--

CREATE TABLE `Conge` (
  `id` int(11) NOT NULL,
  `date` datetime DEFAULT CURRENT_TIMESTAMP,
  `dateDebut` date DEFAULT NULL,
  `dateFin` date DEFAULT NULL,
  `numero` int(11) NOT NULL,
  `status` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL,
  `typeConge_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `Conge`
--

INSERT INTO `Conge` (`id`, `date`, `dateDebut`, `dateFin`, `numero`, `status`, `employe_id`, `typeConge_id`) VALUES
(1, '2017-07-06 10:46:30', NULL, NULL, 0, NULL, NULL, NULL),
(2, '2017-07-06 10:46:34', NULL, NULL, 0, NULL, NULL, NULL);

-- --------------------------------------------------------

--
-- Table structure for table `EMPLOYE`
--

CREATE TABLE `EMPLOYE` (
  `id` int(11) NOT NULL,
  `nom` varchar(255) DEFAULT NULL,
  `post_id` int(11) DEFAULT NULL,
  `userInfo_login` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `EMPLOYE`
--

INSERT INTO `EMPLOYE` (`id`, `nom`, `post_id`, `userInfo_login`) VALUES
(1, 'foulen', NULL, 'user'),
(2, 'falten', NULL, 'user');

-- --------------------------------------------------------

--
-- Table structure for table `POST`
--

CREATE TABLE `POST` (
  `id` int(11) NOT NULL,
  `code` varchar(255) DEFAULT NULL,
  `intituler` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL,
  `id_superieur` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `TypeConge`
--

CREATE TABLE `TypeConge` (
  `id` int(11) NOT NULL,
  `code` int(11) NOT NULL,
  `intituler` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `TypeConge`
--

INSERT INTO `TypeConge` (`id`, `code`, `intituler`) VALUES
(1, 5757, 'Annuel'),
(2, 2, 'Maladie');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `login` varchar(255) NOT NULL,
  `country` varchar(255) DEFAULT NULL,
  `enabled` smallint(6) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `role` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`login`, `country`, `enabled`, `password`, `role`, `employe_id`) VALUES
('admin', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_ADMIN', NULL),
('user', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_USER', 1);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `Conge`
--
ALTER TABLE `Conge`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKov9s99mmo220hv4d8ppeobut` (`employe_id`),
  ADD KEY `FKocvumeoahniu5uvsjlwq5mvnp` (`typeConge_id`);

--
-- Indexes for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKpe9llqbqsni2xqg1vms2h716j` (`post_id`),
  ADD KEY `FKioxxmg7s2j18x2fo7ahnclmcd` (`userInfo_login`);

--
-- Indexes for table `POST`
--
ALTER TABLE `POST`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKl6st4h0ujkdiun27r7tak7t7n` (`employe_id`),
  ADD KEY `FK3x5ro8omrg46k9jaihue19o8q` (`id_superieur`);

--
-- Indexes for table `TypeConge`
--
ALTER TABLE `TypeConge`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`login`),
  ADD KEY `FK82xfucsr861ymb3t5sp2hulo3` (`employe_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `Conge`
--
ALTER TABLE `Conge`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `POST`
--
ALTER TABLE `POST`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `TypeConge`
--
ALTER TABLE `TypeConge`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `Conge`
--
ALTER TABLE `Conge`
  ADD CONSTRAINT `FKk1p9i6bbic92cgaad05k4smsg` FOREIGN KEY (`id`) REFERENCES `EMPLOYE` (`id`),
  ADD CONSTRAINT `FKlm34pn7fv5rcolnjgjyjnuj9l` FOREIGN KEY (`id`) REFERENCES `TypeConge` (`id`),
  ADD CONSTRAINT `FKocvumeoahniu5uvsjlwq5mvnp` FOREIGN KEY (`typeConge_id`) REFERENCES `TypeConge` (`id`),
  ADD CONSTRAINT `FKov9s99mmo220hv4d8ppeobut` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);

--
-- Constraints for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  ADD CONSTRAINT `FKioxxmg7s2j18x2fo7ahnclmcd` FOREIGN KEY (`userInfo_login`) REFERENCES `users` (`login`),
  ADD CONSTRAINT `FKpe9llqbqsni2xqg1vms2h716j` FOREIGN KEY (`post_id`) REFERENCES `POST` (`id`);

--
-- Constraints for table `POST`
--
ALTER TABLE `POST`
  ADD CONSTRAINT `FK3x5ro8omrg46k9jaihue19o8q` FOREIGN KEY (`id_superieur`) REFERENCES `POST` (`id`),
  ADD CONSTRAINT `FKl6st4h0ujkdiun27r7tak7t7n` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);

--
-- Constraints for table `users`
--
ALTER TABLE `users`
  ADD CONSTRAINT `FK82xfucsr861ymb3t5sp2hulo3` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;





我尝试了什么:



我注意到插入后在我的表conge中恰好有两行我得到了一个外键问题。我已经使用了hibernate和phpmyadmin来插入,插入两行后我总是遇到同样的问题。



What I have tried:

I notice that after inserting exactly two row in my table "conge" I get a foreign key problem. I have used hibernate and also the phpmyadmin to insert and I have always the same problem after inserting two row.

推荐答案




这篇关于带有hibernate的外键和mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:26