-->
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: how to save particular entity object only
PostPosted: Tue Jan 05, 2010 12:22 pm 
Newbie

Joined: Tue Jan 05, 2010 9:04 am
Posts: 2
Hi guys,

I am new to nhibernate. I have a simple customer table with ID and Name column. I have done all the nhibernate mapping and every thing is working fine. The layout of the form is i have a text box which accepts customer name, there are three buttons Save, Clear and Delete. There is a datagridview control on the form which shows existing records from customer table.
While loading the form system gets customer collection through nhibernate and displays in grid.
Save button is creates a new customer object and save it.
Delete button deletes selected customer.
Clear button deletes all the customers.

My grid is editable.
If i change something in the grid and then goto customer textbox type some value and click on save button then system creates a new customer with the name i typed in the textbox ( till this point is fine) and it also saves the changes i made in the grid.

Now my question is how i can avoid saving grid data. I know if disable grid then user will not be able to edit, but i want to save only newely added record.

Here is the code i am using to save new entity object which also saves the grid data

private void button1_Click(object sender, EventArgs e)
{
Customer c = new Customer();

using (ITransaction tx = session.BeginTransaction())
{
c.Name = txtName.Text;
object obj = session.Save(c);
tx.Commit();
MessageBox.Show(c.ID.ToString());
FillGrid();
}
}


Top
 Profile  
 
 Post subject: Re: how to save particular entity object only
PostPosted: Wed Jan 06, 2010 12:04 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The other objects are probably still in the session. You can clear the session after loading.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: how to save particular entity object only
PostPosted: Mon Jan 18, 2010 11:32 am 
Newbie

Joined: Tue Jan 05, 2010 9:04 am
Posts: 2
Can anybody explain solution to this problem, it would be nice to add some example...

thanks,
Naim


Top
 Profile  
 
 Post subject: Re: how to save particular entity object only
PostPosted: Sat Jan 23, 2010 5:07 am 
Newbie

Joined: Tue Jan 05, 2010 12:15 pm
Posts: 9
After the first fetch, close the session, this should cause all other objects to be detached, since you do not want any update this should be fine.
Code:
private void button1_Click(object sender, EventArgs e)
{
  Customer c = new Customer();
  using (ISession session = factory.OpenSession())
  {
    using (ITransaction tx = session.BeginTransaction())
    {
      c.Name = txtName.Text;
      object obj = session.Save(c);
      tx.Commit();
      MessageBox.Show(c.ID.ToString());
      FillGrid();
    }
  }
}


An other approach would be to mark those properties as immutable so they can be filled only once and not be updated.
Code:
<property
        name="propertyName"
        column="column_name"
        type="typename"
        update="true|false"
        insert="true|false"
        formula="arbitrary SQL expression"
        access="field|property|ClassName"
/>

set update to false


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.