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

  • SEARCH
  • Home
  • 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 3945840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:03:45+00:00 2026-05-20T01:03:45+00:00

We are mapping the primary key of an object like this: Id(x => x.Id,

  • 0

We are mapping the primary key of an object like this:

Id(x => x.Id, "ID").GeneratedBy.Native("SEQUENCENAME");

We have business logic depending on certain ids to exist (legacy, not easily changed). New objects should get generated ids from an Oracle sequence, but there are always rows with known ids.

We’re using SQLite for unit testing and I need to persist new objects to the in-memory database with these known ids. This will not work with any of the following methods:

session.Replicate(objectWithKnownId, <any replication mode>);
session.Merge(objectWithKnownId)

According to nHibernate documentation, the Replicate method seems to be what I’m looking for.

Persist all reachable transient
objects, reusing the current
identifier values.

When using it with SQLite, however, I will only get generated ids. Can anyone think of a good way of solving this?

  • 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-20T01:03:46+00:00Added an answer on May 20, 2026 at 1:03 am

    After looking into this problem and reading the respons from AlexCuse (+1 to his answer), I deemed it was not possible to use the native id generator in this case. I both needed unit tests to work when saving rows with known ids in test setups and tests inserting with autogenerated ids.

    One option was to have some sort of check in the fluent mapping that would use GeneratedBy.Native("SEQUENCENAME") in production code and GeneratedBy.Assigned in tests, but I didn’t like the idea of having differences related to NHibernate mappings between unit tests and production.

    What I opted for in the end was to handle this in the repository. I have an Add method in the relevant repository and this will handle assigning a generated id from a sequence if the id isn’t already set, something like this:

    public void Add(TheClass newObject) {
        if (newObject.Id == 0) {
           newObject.Id = sequenceGenerator.GetNextValue("SEQUENCENAME");
        }
        session.Save(newObject);
    }
    

    In unit tests I will insert a mock sequence generator in the repository. You could argue that this is similar to the approach of having different mappings for unit tests and production code, but I think this approach makes the difference a bit more isolated. The most important reason, though, is that it allows me to use both assigned and automatically generated ids also in unit tests.

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

Sidebar

Related Questions

I have a function like this for binding the primary key from the selected
I have situation where I am mapping table's columns to the primary key of
I have some troubles mapping a JSON Array to RestKit. This is what the
I have a table which uses two columns to represent its primary key, a
This post is an continuation of this post I have DlUser Class each object
We have some legacy database mappings in GORM and several have a primary key
I have a method like this public static DataSet GetAllDataBaseNames() { //Instance of connection
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have a mapping of String id -> Object . Apart from merely having
I have a database that contains a table that looks a bit like this:

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.