Domanda

I use NHibernate with the Repository-Pattern. The Repositories has the Methods Insert() and Update(). Now, Insert() call Session.Save(entity) and Update() call Session.Merge(entity). My Entities has for the PrimaryKey the fallowing mapping:

    <id name="Id" column="TBADRPERSON_ID" type="Int32" unsaved-value="0">       
                    <generator class="assigned"/>
    </id>

The id is assigned from me. Now, Session.Save(entity) is first performing an Select to see, if the Entity is new or already exisiting. But in the Insert()-Case the entity is always new. I am searching now a way to tell NHibernate to always do an Insert - instead first a Select and then an Insert. I have seen, that I could do this with a Version-Property - but I cannot change the Database-Schema.

Thanks for any help.

È stato utile?

Soluzione

If you want to always save, maybe you can try :

<id name="Id" column="TBADRPERSON_ID" type="Int32" unsaved-value="any">       
                <generator class="assigned"/>
</id>

Then do a Session.SaveOrUpdate() for Insert(), and a Session.Merge() then Session.SaveOrUpdate() for Update()

Altri suggerimenti

I think you should delete the property unsaved-value="0". when you save entity , Nhibernate will compare the id with '0'. if there are not equal,Nhibernate selects it first if it's not in session.

I think you can use session.Update() instead of session.Merge(), it won't be load an entity from DB. Or probably, you can use session.SaveOrUpdate() in all cases.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top