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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:31:53+00:00 2026-06-17T11:31:53+00:00

I have a simple application where users can open orders, make changes to them,

  • 0

I have a simple application where users can open orders, make changes to them, then save them back to the database. When an order is opened, I retrieve it from the database like this

ISession session = SessionFactory.OpenSession();

...

Order order = session.Query<Order>()
                     .Where(o => o.Id == id)
                     .FirstOrDefault();

To enable lazy loading of order.Comments, I keep the session open until the order is closed. Here are the mappings:

<class name="Order">
    <id name="Id">
        <generator class="identity" />
    </id>    

...

    <set name="Comments" access="field.camelcase-underscore" cascade="all-delete-orphan"    lazy="true" order-by="Created">
      <key column="OrderId" />
      <one-to-many class="Comment" />
    </set>     
</class>

<class name="Comment" table="OrderComment" lazy="false">
    <id name="Id">
      <generator class="identity" />
    </id>
    <many-to-one name="Author" />
    <property name="Created" />
    <property name="Text" length="1000" />
</class>

The app is designed so that while an order is open, it can be saved several times before it is closed. I save like this:

using (ITransaction trans = session.BeginTransaction())
{
    session.SaveOrUpdate(order)
    trans.Commit();
}

Finally, when the user closes the order, I dispose of the session.

Here’s there problem: If the user adds a comment, saves, and then before closing the order, adds another comment and saves again, the first comment is removed during the second save. Here’s the sql that is outputted from the second save:

NHibernate: INSERT INTO OrderComment (Id, Author, Created, Text) VALUES (hibernate_sequence.nextval, :p0, :p1, :p2) returning Id into :nhIdOutParam;:p0 = 1 [Type: Int32 (0)], :p1 = 14.01.2013 12:53:20 [Type: DateTime (0)], :p2 = '2' [Type: String (0)], :nhIdOutParam = NULL [Type: Int32 (0)]

**NHibernate: UPDATE OrderComment SET OrderId = null WHERE OrderId = :p0 AND Id = :p1;:p0 = 465 [Type: Int32 (0)], :p1 = 591 [Type: Int32 (0)]**

NHibernate: UPDATE OrderComment SET OrderId = :p0 WHERE Id = :p1;:p0 = 465 [Type: Int32 (0)], :p1 = 592 [Type: Int32 (0)]

So the problem is that line in bold – the OrderId for the first comment is being set to null. Can anybody tell my why?

Is there anything wrong with the way I’m using nHibernate here? To reiterate what I do:

  1. Open a session
  2. Retrieve an object
  3. User updates the object
  4. Save the object by beginning a transaction, calling session.SaveOrUpdate(object), then committing the transaction.
  5. Repeat steps 3 and 4 any number of times.
  6. Dispose of the session.

Is this an acceptable way to use nHibernate?

Edit

Here is the comments property in the Order class:

ICollection<Comment> _comments = new List<Comment>();
public virtual ReadOnlyCollection<Comment> Comments
{
    get 
    {
        return _comments.ToList().AsReadOnly(); 
    }
}

Then comments are added by calling the following method:

public virtual void AddComment(Comment comment)
{
    _comments.Add(comment);    
}

 ...

Comment comment = new Comment()
{
    Author = User.Current,
    Created = DateTime.Now,
    Text = text
};

order.AddComment(comment);

Here is the Comment class. Id is implemented in the base class PersistentObject<T>:

public class Comment : PersistentObject<int>
{        
    public User Author { get; private set; }
    public DateTime Created { get; private set; }
    public string Text { get; private set; }                                             
}

public abstract class PersistentObject<T>
{        
    public virtual T Id { get; protected internal set; }       

    public override bool Equals(object obj)
    {
        // If both objects have not been saved to database, then can't compare Id because this
        // will be 0 for both.  In this case use reference equality.

        PersistentObject<T> other = obj as PersistentObject<T>;
        if (other == null)
            return false;

        bool thisIsDefault = object.Equals(Id, default(T));
        bool otherIsDefault = object.Equals(other.Id, default(T));

        if (thisIsDefault && otherIsDefault)
            return object.ReferenceEquals(this, other);
        else if (thisIsDefault || otherIsDefault)
            return false;
        else
            return object.Equals(this.Id, other.Id);
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }        
}
  • 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-06-17T11:31:54+00:00Added an answer on June 17, 2026 at 11:31 am

    I don’t think that the HashCode should be taken from the Id. Order.Comments is a set, which is base on the hash. The comments all have an Id 0 at the beginning. The HashCode is taken when adding to the collection, when it is 0.

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

Sidebar

Related Questions

I have a simple application with 'game' and 'article' pages that users can share
I have SQL Server database for users, and now I want to make simple
I have created a simple Eclipse RCP application where I can open multiple editor
I'm currently working on a simple web application where users each have their own
I have an application writted in ASP with a simple form. The users fill
In my web application, I have users input a date in a simple textbox.
The users of my web application may have more than one browser window open
i have an Android open source application published here https://github.com/evilsocket/dsploit ... as you can
I have a simple iPhone application which uses OpenGL ES (v1) to draw a
I have a simple WPF application that uses ClickOnce to handle installing. Within 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.