NHibernate still issues an update after insertion

I have very simple unidirectional mappings. see below:

public ContactMap() { Id(x => x.Id).GeneratedBy.Assigned(); Map(x => x.Name); References(x => x.Device); HasMany(x => x.Numbers) .Not.Inverse() .Not.KeyNullable() .Cascade.AllDeleteOrphan() .Not.LazyLoad() .Fetch.Subselect(); Table("Contacts"); } public PhoneNumberMap() { Id(x => x.Id).GeneratedBy.Native(); Map(x => x.Number); Table("ContactNumbers"); } 

According to this message, after nhibernate 3 and above, the installation key as invalid should fix the problem with updating the insert (the problem is when NHibernate issues an insert with a foreign key set to null and then updating to update the foreign key to fix the value), but for this is not me. When I set the key as invalid, NHibernate issues the correct insert statement

 INSERT INTO ContactNumbers (Number, ContactId) VALUES ('(212) 121-212' /* @p0 */, 10 /* @p1 */); 

As you can see, it inserts the ContactId field, but after that it still issues the update instruction

 UPDATE ContactNumbers SET ContactId = 10 /* @p0 */ WHERE Id = 34 /* @p1 */ 

So, to clarify the problem. NHibernate inserts a contact string with the foreign key assigned correctly, and after that it issues an update instruction to update the foreign key (ContactId), which is redundant.

How can I get rid of this redundant update statement? Thanks.

By the way, I am using the latest version of NHibernate and Fluent NHibernate. SQLite Database

+8
mapping nhibernate fluent-nhibernate
source share
5 answers

You must set "updatable"=false on your key to prevent updating.

 public ContactMap() { Id(x => x.Id).GeneratedBy.Assigned(); Map(x => x.Name); References(x => x.Device); HasMany(x => x.Numbers) .Not.Inverse() .Not.KeyNullable() .Not.KeyUpdate() // HERE IT IS .Cascade.AllDeleteOrphan() .Not.LazyLoad() .Fetch.Subselect(); Table("Contacts"); } 
+20
source share

You cannot on 3.2.0 BETA.

In v3.2.0 BETA, the one-to-many improvement introduced this anomaly in a one-to-many one-to-many relationship (I'm not really sure if the anomaly is what you would call it).

Before 3.2, you will need to set a foreign key to allow null for this type of relationship. So I would ignore the fact that this is happening and just go with it. Otherwise, you will need to change it to a completely bidirectional connection.

  • [NH-941] - one-many requiring zero foreign keys

Release Notes or JIRA Issue

edit Also, the answer to the message you are pointing to is to fix save null-save-update , and not to install an additional update

+4
source share

I do not know if you can really get rid of it.

Try using a different id generator as native. This forces NH to insert the record only to get the identifier. An identifier is used for each object in the session, so it cannot insert an insert later. This may lead to future updates. Use hi-lo or something similar.

Edit

Why aren't you using the component in this case? You do not need to provide a phone number separately if they consist only of a number. Something like this (I'm not an FNH user, so this might be wrong):

  public ContactMap() { Id(x => x.Id).GeneratedBy.Assigned(); Map(x => x.Name); References(x => x.Device); HasMany(x => x.Numbers) .Not.Inverse() .Not.KeyNullable() .Cascade.AllDeleteOrphan() .Not.LazyLoad() .Fetch.Subselect() .Component(c => { Map(x => x.Number); }) .Table("ContactNumbers"); Table("Contacts"); } 
+3
source share

Try setting inverse to true for display and assign a link in the code.

inverse means that the child is responsible for storing the parent id.

eg.

 var contact = new Contact(); var phoneNumber = new PhoneNumber(); phoneNumber.Contact = contact; 

That way, when you insert a PhoneNumber record, NH can insert ContactId without the need for a separate update.

What I did in NH 2, I would suggest that the behavior still works the same in 3.

+3
source share

This is what Trevor Pilly said. Use inverse="true" . If you decide not to have inverse="true" , this is a consequence of this choice. You cannot have it in both directions.

+1
source share

All Articles