-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: foreign key and mysql with hibernate
PostPosted: Thu Jul 06, 2017 6:24 am 
Newbie

Joined: Thu Jul 06, 2017 6:21 am
Posts: 1
Hi,

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.

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.

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


Code:
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 */;


Top
 Profile  
 
 Post subject: Re: foreign key and mysql with hibernate
PostPosted: Sun Jul 16, 2017 12:15 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Maybe you don't have that employee id and the insert fails.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.