Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3488844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:19:16+00:00 2026-05-18T11:19:16+00:00

I have 2 tables (Item and Code) with relation one to many(one Item can

  • 0

I have 2 tables (Item and Code) with relation one to many(one Item can have many Codes – this is implemented on db level with foreign key).

When I am trying to insert one Item record and several Code records(they are linked to that Item record) in one transaction using spring JDBC I get

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mrbcodes`.`code`, CONSTRAINT `code_item_fk` FOREIGN KEY (`itemId`) REFERENCES `item` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

Here is my insert into db code.

public void saveCode(Code code, long itemId) {
  String insertSql = "insert into code(number, value, itemId) values (:number,:value,:itemId)";
  SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(code);
  log.info("Inserting values into code table; number:" + code.getNumber() +"; value:" + code.getValue() 
    + "; itemId:" + itemId);
  simpleJdbcTemplate.update(insertSql, namedParameters);
 }

 @Transactional
 public void saveCodes(Collection <Code> codes) {
  KeyHolder keyHolder = new GeneratedKeyHolder();
  insertNewItem(keyHolder);  
  Number key = keyHolder.getKey();
  Assert.notNull(key, "After item insert; returned key must not be null");
  for (Code code:codes) {
   saveCode(code, key.longValue());
  }
 }

 private KeyHolder insertNewItem(KeyHolder keyHolder) {
  String createItemSQL = "insert into item(timeUsed) values(:defaultValue)";  
  SqlParameterSource sqlParameterSource = new MapSqlParameterSource("defaultValue", defaultValue);
  log.debug("Inserting new item");
  simpleJdbcTemplate.getNamedParameterJdbcOperations().update(createItemSQL,sqlParameterSource, keyHolder);
  log.debug("Item inserted; keyHolder " + keyHolder);
  return keyHolder;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:19:17+00:00Added an answer on May 18, 2026 at 11:19 am

    You don’t use itemId in saveCode, therefore Code is saved without itemId.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following three tables: Item Code ItemCode (many to many relationship between
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have two tables, content and images (and a ContentImages table for the one
I have two tables (item and category, I think they speak for themselves) and
I have 3 tables: Item - which is the DataContext - it has a
I have two tables like the below one in my DB. In the first
I have defined three tables, Stores, InventoryItems and StoreItemRecords. My StoreItemRecords table has foreign
If all tables I want to delete from have the column gamer_id can i
I have the following tables structure: Item: Id, Description Tags: Id, TagName ItemXrefTag Id,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.