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:
- Is there an easy way to bend nHibernate to my will?
- 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?
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:
The Owner retrieved from the session by Get will have the newOwner properties but also be valid for the session.