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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:58:07+00:00 2026-05-22T23:58:07+00:00

** This question has been edited to make it simpler and more focused **

  • 0

** This question has been edited to make it simpler and more focused **

Employee has an EmployeeNumberValue property which I would like to have auto-incremented by the db. To the business domain, this is a unique id assigned to employees and used to identify them on employee cards, etc. To the database however, it is an alternate id and not the primary key.

NHib has a documented ability called Generated Properties.
Per the docs, “generated properties are properties which have their values generated by the database. Typically, NHibernate applications needed to Refresh objects which contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to NHibernate. Essentially, whenever NHibernate issues an SQL INSERT or UPDATE for an entity which has defined generated properties, it immediately issues a select afterwards to retrieve the generated values.”

The problem I am having is that while NHib is making the additional SELECT to update the EmployeeNumberValue, it is not assigning the retrieved value to the property.

Can anyone see why this is happening what the fix is?

Cheers,
Berryl

FAILING TEST AND OUTPUT (tested w/ SQLite in memory db):

    [Test]
    public void Employee_OnInsert_EmployeeNumberValueIsIncremented() {

        var emp1 = new Employee
        {
            FullName = _fullName,
            Department = _department,
        };
        var emp2 = new Employee
        {
            FullName = _fullName,
            Department = _department,
        };

        var session = _SessionFactory.GetCurrentSession(); 

        using (var tx = session.BeginTransaction())
        {
            session.Save(_department);
            session.Save(emp1);
            session.Save(emp2);
            tx.Commit();
        }
        Assert.That(emp1.EmployeeNumberValue, Is.EqualTo(1));
        Assert.That(emp2.EmployeeNumberValue, Is.EqualTo(2));
    }

NHibernate: INSERT INTO Employees (FirstName, LastName, DepartmentId, EmployeeId) 
        VALUES (@p0, @p1, @p2, @p3);@p0 = 'Berryl' [Type: String (0)], @p1 = 'Hesh' [Type: String (0)], @p2 = 32768 [Type: Int32 (0)], @p3 = 65536 [Type: Int32 (0)]
NHibernate: SELECT employee_.EmployeeNumberValue as Employee2_1_ FROM Employees employee_ WHERE employee_.EmployeeId=@p0;@p0 = 65536 [Type: Int32 (0)]
NHibernate: INSERT INTO Employees (FirstName, LastName, DepartmentId, EmployeeId) 
        VALUES (@p0, @p1, @p2, @p3);@p0 = 'Berryl' [Type: String (0)], @p1 = 'Hesh' [Type: String (0)], @p2 = 32768 [Type: Int32 (0)], @p3 = 65537 [Type: Int32 (0)]
NHibernate: SELECT employee_.EmployeeNumberValue as Employee2_1_ FROM Employees employee_ WHERE employee_.EmployeeId=@p0;@p0 = 65537 [Type: Int32 (0)]
Test failed: 
   Expected: 1
   But was:  0

OBJECT MODEL

public class Employee : Entity, IResource
{
    public virtual long EmployeeNumberValue { get; set; }

    ...
}

MAPPING:

  <class name="Employee" table="Employees">

<id name="Id" unsaved-value="0">
  <column name="EmployeeId" />
  <generator class="hilo" />
</id>

<property name="EmployeeNumberValue" generated="insert" insert="false" update="false" >
  <column name="EmployeeNumberValue" sql-type="int IDENTITY(1,1)" index="IDX_EmployeeNumber"  />      
</property>

...

create table Employees (
    EmployeeId INTEGER not null,
   EmployeeNumberValue int IDENTITY(1,1),
   FirstName TEXT not null,
   LastName TEXT not null,
   DepartmentId INTEGER,
   primary key (EmployeeId)
)

I suspect the way I am marking the column as IDENTITY is also suspect. I tried using database-object as below, but got a usage error in doing so

  <database-object>
    <create>
      ALTER TABLE Employee DROP COLUMN EmployeeNumberValue
      ALTER TABLE Employee ADD EmployeeNumberValue INT IDENTITY
    </create>
    <drop>
      ALTER TABLE Employee DROP COLUMN EmployeeNumberValue
    </drop>
  </database-object>

SQLiteException : SQLite error  "DROP": syntax error
  • 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-22T23:58:08+00:00Added an answer on May 22, 2026 at 11:58 pm

    From a design perpective I wouldn’t rely on NHibernate in this case. What I mean is, that in your domain model, you want an employee to get a new employee card number.

    In this case I would only allow an employee to be instantiated if there is a card number.

    public class EmployeeCardNumber
    {
        private string id = String.Empty;
        internal EmployeeCardNumber(string id)
        {
            this.id = id;
        }
    }
    
    
    public class Employee
    {
        private EmployeeCardNumber employeeCardNumber;
    
        public EmployeeCardNumber CardNumber { ... }
    
        public Employee(EmployeeCardNumber employeeCardNumber)
        {
            this.employeeCardNumber = employeeCardNumber;
        }
    }
    

    So now you have to think about how to generate a unique EmployeeCardNumber.

    public class EmployeeCardNumberFactory
    {
        public EmployeeCardNumber CreateNew()
        {
            // in this example the card number will be a guid.
            // but you could also implement a "EmployeeCardNumberGenerator" class which will do crazy database stuff
            return new EmployeeCardNumber(Guid.NewGuid().ToString());
        }
    }
    

    Then you would later do:

    EmployeeCardNumber cardNumber = employeeCardNumberFactory.CreateNew();
    Employee employee = new Employee(cardNumber, name, etc...);
    

    Addition:
    To generate a “EmployeeCardNumber” via database, you could just map “EmployeeCardNumber” to an extra table “EmployeeCardNumber” that will serve as your identity generator like:

    <class name="EmployeeCardNumber" table="EmployeeCardNumber">
        <id name="id" access="field" unsaved-value="0">
          <column name="EmployeeCardNumberId" />
          <generator class="identity" />
        </id>
    </class>
    

    Then in the factory you could do:

    public class EmployeeCardNumberFactory
    {
        private IEmployeeCardNumberRepository repository = new EmployeeCardNumberRepository(); // inject...
        public EmployeeCardNumber CreateNew()
        {     
            EmployeeCardNumber cardNumber = new EmployeeCardNumber();
            repository.Save(cardNumber); // gets you a fresh id
            return cardNumber;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been bothering me for a while. If I do int* a
This question has been asked here in one form or another but not quite
This question has been asked before, mainly with reference to ListActivity, and therefore I
This question has been asked in various guises. However. this is a slightly different
This question has been bugging me for a while now. When writing a CSS
This question has been asked before for other languages: Python , PHP and JavaScript
This question covncerns my lack of understanding of how to use the core data
I have a question on CSS media query.. I am implementing a HTML page
there is a great gem for Rails 3 and Google Maps v3 integration (gmaps4rails)
Goal: To display an application in the middle of the screen on a solid

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.