I am using NHibernate & Net Persistence API with C# in my project. I have the following Save method:
public void Save(IGenericEntity entity)
{
_entityManager = GetEm();
_entityManager.GetTransaction().Begin();
_entityManager.Persist(entity);
_entityManager.Flush();
_entityManager.GetTransaction().Commit();
_entityManager.Clear();
}
Now say entity has a field ID (which is auto-generated value from db). Now I am saving this entity, and I want either auto-generated ID or saved entity in return, but.Commit() return type is void. Anyone can please guide about this?
I have used hibernate with Java, and there by calling .save() it returns the saved entity (with auto-generated ID). How to achieve the same in above context?
After the call to #flush your entity will have the id in it. Try this test: