-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Save as new... function with NHibernate
PostPosted: Wed Jan 11, 2006 7:11 am 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
Save as new... function with NHibernate

Hibernate version: 1.0.1.0

Name and version of the database you are using: MS SQL Server 2000 with SP4

I want to implement a Save As New function in my application. It's my way:

+ Load all data and bind to a Model class
+ Apply changing to current model
+ Create new model and copy information from current model
+ Insert new model into database

My problem is, I only insert new model, but NHibernate also update the current model to database. How can I do insert only without updating to current model?

You can try my simple program here. It also contains SQL script. Prefer readme.txt if you want more information.

Thanks a lot!


Top
 Profile  
 
 Post subject: Try session.Evict
PostPosted: Thu Jan 12, 2006 12:40 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
What is happening is that when you call session.save(newModel) it is actually only saving your newModel. But when you call tx.commit() it flushes the session and since your old model is still attached to the session and is dirty (since you added a new line to the old model) it fires another insert for the old model.

If you do not want to save the old model when you call tx.Commit() you can call session.Evict(model) to detatch the old model from the session, in this case your old model will not be saved.
Hope this helps.


Top
 Profile  
 
 Post subject: Re: Try session.Evict
PostPosted: Thu Jan 12, 2006 11:54 pm 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
samujob wrote:
What is happening is that when you call session.save(newModel) it is actually only saving your newModel. But when you call tx.commit() it flushes the session and since your old model is still attached to the session and is dirty (since you added a new line to the old model) it fires another insert for the old model.

If you do not want to save the old model when you call tx.Commit() you can call session.Evict(model) to detatch the old model from the session, in this case your old model will not be saved.
Hope this helps.


It works! Thanks a lot! :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 9:35 am 
Newbie

Joined: Mon Jan 09, 2006 4:57 am
Posts: 18
I found another problem when I try to implement Save as new function.

After I create a new model from current model, if I use session object to populate a datatable like this

Code:
DataTable table = DBHelper.GetCodeDetails(session);


then try to insert new model

Code:
ITransaction tx = session.BeginTransaction();
session.Evict(currentModel);
session.Save(newModel);
tx.Commit();


NHibernate will update currentModel also.

If I remove

Code:
DataTable table = DBHelper.GetCodeDetails(session);


NHibernate only insert new model, do not update current model.

How can I fix it? Thanks!

This is code of DBHelper

Code:
using System;
using System.Collections;
using System.Data;
using System.Text;

using NHibernate;

namespace BugProject.Utilities
{
    class DBHelper
    {
        public static DataTable GetCodeDetails(ISession Session)
        {
            DataTable table = null;

            string sql = "";

            sql = "SELECT M.Id,  ";
            sql += "       M.Info ";
            sql += "FROM Model AS M ";

            try
            {
                IList codeDetails = Session.Find(sql);

                table = GenerateDataTable(codeDetails);
            }
            catch (HibernateException ex)
            {
                throw ex;
            }

            return table;
        }

        private static DataTable GenerateDataTable(IList List)
        {
            DataTable table = null;

            if (List != null)
            {
                table = new DataTable();

                // Declare DataColumn and DataRow variables.
                DataColumn column;
                DataRow row;

                // Create new DataColumn, set DataType, ColumnName
                // and add to DataTable.   
                column = new DataColumn();
                column.DataType = Type.GetType("System.Int32");
                column.ColumnName = "Id";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "Info";
                table.Columns.Add(column);

                IEnumerator e = List.GetEnumerator();

                while (e.MoveNext())
                {
                    object[] objs = (object[])e.Current;

                    row = table.NewRow();
                    row["Id"] = objs[0];
                    row["Info"] = objs[1];
                    table.Rows.Add(row);
                }
            }

            return table;
        }

    }
}


You can try my Source code


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.