How do I Insert or Update (or overwrite) a record using NHibernate?

后端 未结 6 1537
忘掉有多难
忘掉有多难 2021-02-02 00:51

I need to write a row to the database regardless of whether it already exists or not. Before using NHibernate this was done with a stored procedure. The procedure would attempt

6条回答
  •  梦如初夏
    2021-02-02 01:33

    You can do

    Obj j = session.get(Object.class(), id);
    if (j != null)
       session.merge(myObj);
    else
       session.saveOrUpdate(myObj);
    

提交回复
热议问题