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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:25:46+00:00 2026-05-18T09:25:46+00:00

My project has a Ticket entity with an OwnedBy property. I’m using nHibernate to

  • 0

My project has a Ticket entity with an OwnedBy property. I’m using nHibernate to persist the tickets to a database.

The canonical source for potential ticket owners is Active Directory. Since I don’t want to have to query Active Directory every time I load tickets, I also persist Ticket.OwnedBy to the database and load it from there when fetching tickets.

When a ticket’s owner is reassigned, I get the new Owner from Active Directory and assign it to Ticket.OwnedBy, then call Session.SaveOrUpdate(ticket). When I commit the transaction, NHibernate throws a NonUniqueObjectException because an Owner with the same ID is already associated with the session.

Class definitions

class Ticket {
    public int Id { get; set; }
    public Owner OwnedBy { get; set; }
    /* other properties, etc */
}
class Owner {
    public Guid Guid { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    /* other properties, etc */
}

Fluent nHibernate Mappings

class TicketMap : ClassMap<Ticket> {
    public TicketMap() {
        Id(x => x.Id);
        References(x => x.OwnedBy)
            .Cascade.SaveUpdate()
            .Not.Nullable();
        /* other properties, etc */
    }    
}
class OwnerMap : ClassMap<Owner> {
    public OwnerMap() {
        Id(x => x.Guid)
            .GeneratedBy.Assigned()
        Map(x => x.Name);
        Map(x => x.Email);
        /* other properties, etc */
    }
}

Sample code

// unitOfWork.Session is an instance of NHibernate.ISession
Ticket ticket = unitOfWork.Session.Get<Ticket>(1);
Owner newOwner = activeDirectoryRepo.FindByGuid(/* guid of new owner, from user */);
ticket.OwnedBy = newOwner;
unitOfWork.Session.SaveOrUpdate(ticket);
unitOfWork.Commit(); // Throws NonUniqueObjectException

I want nHibernate to overwrite the properties of the existing Owner with the properties of the unattached one. (The Name or Email in the object I fetched from AD may be different, and AD is supposed to be the canonical source.) I’ve tried calling Session.SaveOrUpdateCopy(ticket.OwnedBy) and Session.Merge(ticket.OwnedBy) before the SaveOrUpdate(ticket), but the exception is still being thrown. I’ve also read this related question about NonUniqueObjectException, but calling Session.Lock() didn’t work either.

I have two questions:

  1. Is there an easy way to bend nHibernate to my will?
  2. I may have made an architectural misstep in trying to treat the owners I fetch from AD as the same type as the owners I store in the DB. How can I improve this design so I won’t need to bend nHibernate to my will?
  • 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-18T09:25:47+00:00Added an answer on May 18, 2026 at 9:25 am

    Merge works, the most likely issue is you did not call it properly. Merge will update the existing object with the new object properties but Merge does not attach the new object. So you have to use the existing one. If you use the new object after a merge you still get the same error.

    The following code should fix the problem:

    //Merge the new Owner
    unitOfWork.Session.Merge(newOwner);
    
    //Get a valid Owner by retrieving from the session
    Owner owner = session.Get<Owner>(newOwner.Id);
    
    // Set the ticket to this owner instance instead of the new one
    ticket.OwnedBy = owner;
    
    unitOfWork.Session.Update(ticket);
    unitOfWork.Commit(); 
    

    The Owner retrieved from the session by Get will have the newOwner properties but also be valid for the session.

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

Sidebar

Related Questions

My project has a file foo that I've been using and checking in with
My project has a reporting module that gathers data from the database in the
I'm using phpactiverecord for this project, I have this db structure: Tables: Tickets, Labels
My project has some unit tests. They all depend on some external service, over
My project has weirdly started failing with no error messages. Ive tried cleaning the
Our project has a trunk. I worked on Branch A, adding several features, and
My project has multiple themes with different colors. I need to skin certain textboxes
The project has been working fine in 2003 but when opening it in 2008
My Project has 2 java files (A.java and B.java in same package). A.java uses
A Project has multiple Tasks, which has multiple Assignments Projects (1-n) -> Tasks (1-n)

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.