NHibernate: How is the identifier updated when a temporary instance is saved?

If I use a session per transaction and call:

session.SaveOrUpdate (entity) fixed:
session.SaveOrUpdateCopy (entity)

.. and the entity is a temporary instance with identifier-Id = 0. Should the next line automatically update the identifier of the object and make the instance permanent? Or he should do it on a transaction. Commit? Or do I need to explicitly state this somehow?

Obviously, the database row identifier (new, as a transitional one) is autogenerated and stored as some number, but I'm talking about the actual instance of the parameter here. What an instance of business logic.


EDIT - Follow-up work related to this issue.

Display:

public class StoreMap : ClassMap<Store> { public StoreMap() { Id(x => x.Id).GeneratedBy.Identity(); Map(x => x.Name); HasMany(x => x.Staff) // 1:m .Cascade.All(); HasManyToMany(x => x.Products) // m:m .Cascade.All() .Table("StoreProduct"); } } public class EmployeeMap : ClassMap<Employee> { public EmployeeMap() { Id(x => x.Id).GeneratedBy.Identity(); Map(x => x.FirstName); Map(x => x.LastName); References(x => x.Store); // m:1 } } public class ProductMap : ClassMap<Product> { public ProductMap() { Id(x => x.Id).GeneratedBy.Identity(); Map(x => x.Name).Length(20); Map(x => x.Price).CustomSqlType("decimal").Precision(9).Scale(2); HasManyToMany(x => x.StoresStockedIn) .Cascade.All() .Inverse() .Table("StoreProduct"); } } 

EDIT2

Class Definitions:

  public class Store { public int Id { get; private set; } public string Name { get; set; } public IList<Product> Products { get; set; } public IList<Employee> Staff { get; set; } public Store() { Products = new List<Product>(); Staff = new List<Employee>(); } // AddProduct & AddEmployee is required. "NH needs you to set both sides before // it will save correctly" public void AddProduct(Product product) { product.StoresStockedIn.Add(this); Products.Add(product); } public void AddEmployee(Employee employee) { employee.Store = this; Staff.Add(employee); } } public class Employee { public int Id { get; private set; } public string FirstName { get; set; } public string LastName { get; set; } public Store Store { get; set; } } public class Product { public int Id { get; private set; } public string Name { get; set; } public decimal Price { get; set; } public IList<Store> StoresStockedIn { get; private set; } } 
+3
source share
4 answers

As for your question, whenever you clear a session, your entity is stored in the database. When you save your (new) object, NHibernate generates an identifier for you using the generator you provided.

Keep in mind that the Identity generator is not recommended (see this post from Ayende ). When you use the Identity generator, your new object is saved to the database when it is saved, even if you do not hide the database. The reason for this is that NHibernate must provide you with an identifier for an object that it cannot do without doing a backward transition to the database.

A better solution would be to use something like a Guid or HiLo generator if you want โ€œnormalโ€ values. Thus, you can save your essence without having to do feedback with the database, which allows you to do much more efficient work (guidance comes to mind).

+3
source

I'm not sure I understand your question. Actual storage in the database occurs when the session is cleared (for example, by committing a transaction). The call to SaveOrUpdate () does not save the object itself, it simply informs the session that the object should be saved when the session is cleared.

Assuming that the identifier of the object is mapped to the identification field in the database and that your mapping tells NHibernate that the identifier is specified by the database, then the identifier specified by the database will be set as the identifier of the object when it is saved.

+2
source

Nhibernate will set the ID property of your object immediately after calling SaveOrUpdate.

+1
source

I noticed that I was saved by calling:

 session.SaveOrUpdateCopy(entity); 

.. which does NOT update the identifier. But changing to:

 session.SaveOrUpdate(entity); 

. Transition ID will be updated.

I probably misunderstood the documentation (?) .. Section 9.4.2 says:

SaveOrUpdateCopy (Object o) ... If this instance is incorrect or does not exist in the database, NHibernate will save it and return it as a new persistent instance.

Is this just me, or is it not like a transition object (unsaved) will be "returned as permanent"? Does this mean with an updated identifier? I would understand how to correctly interpret this sentence (?)

0
source

Source: https://habr.com/ru/post/1338303/


All Articles