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 745697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:36:48+00:00 2026-05-14T09:36:48+00:00

Context I’m building a persistence layer to abstract different types of databases that I’ll

  • 0

Context

I’m building a persistence layer to abstract different types of databases that I’ll be needing. On the relational part I have mySQL, Oracle and PostgreSQL.

Let’s take the following simplified MySQL tables:

CREATE TABLE Contact (
  ID varchar(15),
  NAME varchar(30)
);

CREATE TABLE Address (
  ID varchar(15),
  CONTACT_ID varchar(15),
  NAME varchar(50)
);

I use code to generate system specific alpha numeric unique ID’s fitting 15 chars in this case. Thus, if I insert a Contact record with it’s Addresses I have my generated Contact.ID and Address.CONTACT_IDs before committing.

I’ve created a Unit of Work (amongst others) as per Martin Fowler’s patterns to add transaction support. I’m using a key based Identity Map in the UoW to track the changed records in memory. It works like a charm for the scenario above, all pretty standard stuff so far.

The question scenario comes in when I have a database that is not under my control and the ID fields are auto-increment (or in Oracle sequences). In this case I do not have the db generated Contact.ID beforehand, so when I create my Address I do not have a value for Address.CONTACT_ID. The transaction has not been started on the DB session since all is kept in the Identity Map in memory.

Question: What is a good approach to address this? (Avoiding unnecessary db round trips)

Some ideas:
Retrieve the last ID: I can do a call to the database to retrieve the last Id like:

SELECT Auto_increment FROM information_schema.tables WHERE table_name='Contact'; 

But this is MySQL specific and probably something similar can be done for the other databases. If do this then would need to do the 1st insert, get the ID and then update the children (Address.CONTACT_IDs) – all in the current transaction context.

  • 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-14T09:36:48+00:00Added an answer on May 14, 2026 at 9:36 am

    Avoid explicitly referencing the CONTACT_ID entirely. Assuming that Contact.NAME has a UNIQUE constraint and that the CONTACT_ID column REFERENCES Contact(ID):

    INSERT INTO Contact (NAME) VALUES ('Joe Bloggs'); -- Contact.ID auto-generated
    INSERT INTO Address (CONTACT_ID, NAME)
         VALUES ((SELECT ID FROM Contact WHERE NAME = 'Joe Bloggs'),
                 '123 Apple Lane');
    

    Now Address.CONTACT_ID is correct without your code knowing the key’s value or even its type.

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

Sidebar

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.