-->
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.  [ 6 posts ] 
Author Message
 Post subject: Transaction management
PostPosted: Mon Apr 17, 2006 10:46 pm 
Newbie

Joined: Sun Apr 16, 2006 11:46 pm
Posts: 3
Hi All,

i'm using NHibernate in my project(C# Winforms Application). I have a requirment to populate some data in the database(SQLserver) with data integrity.

I have two tables.

EMPLOYEE
EmpId
Name

EMPLOYEEADDRESS
EmpId
Address
Addresstype.

The employee employeeaddress relationship is one t many. In a single transaction I want to insert one employee record in employee table and multiple address for same employee in employeeAddress table.

Can anyone help me


Thanking you
Satheesh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 5:53 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
ISession.BeginTransaction and ITransaction.Commit() or ITransaction.Rollback()

Gert


Top
 Profile  
 
 Post subject: It is not working
PostPosted: Tue Apr 18, 2006 8:46 am 
Newbie

Joined: Sun Apr 16, 2006 11:46 pm
Posts: 3
I can populate the data in two tables with different transactions. But in a single transaction i'm not able to, so the data integrity will lost.

I want to save all the data at the same time other wise roll back the transaction

Please give me some samples with with an example i mentioned in the question.


regards
Satheeshj


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 8:59 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
It is hard to understand where is Your problem. But usual transaction handling code is:

Code:
ISession ses = sesskionfactory.OpenSession();
try
{
  ITransaction tx = ses.BeginTransaction();
  try
  {
    // Add/modify objects through session "ses"
    tx.Commit();
  }
  catch
  {
    tx.Rollback()
    throw;
  }
}
finally
{
  ses.Close();
}


You should read the NHibernate documentation, there is some information...

Gert


Top
 Profile  
 
 Post subject: Multile insert in a single transaction
PostPosted: Wed Apr 19, 2006 11:42 pm 
Newbie

Joined: Sun Apr 16, 2006 11:46 pm
Posts: 3
i have Two objects
Member (mId,mName)
memberAddress(aId(auto generated in db),mId,Address,addresstype)


Member mr=new Member();
memberAddress [] m=new memberAddress[3];
mr.mId=1;
mr.mName="Sam";
memberAddress m1=new memberAddress();
memberAddress m2=new memberAddress();
m1.mId=1;
m1.address="mm@m.com";
m1.type="Mail";
m2.mId=1;
m2.address="#888";
m2.type="Home";
m[0]=m1;
m[1]=m2;
mem.SaveMember(m,mr);

and my save member mthod is

public void SaveMember(memberAddress [] address,Member mem)
{
ITransaction tx = null;
try
{
if(!session.IsConnected)
{
session.Reconnect();
}

tx = session.BeginTransaction();
session.Save(mem);
foreach(memberAddress adr in address)
{
session.Save(adr);
}
tx.Commit();
session.Disconnect();

}
catch (Exception ex)
{
tx.Rollback();

session.Disconnect();
// handle exception
}
}



The above is my factory code and it is not working.

I think now you can understand more about my problem. I want to save one master table entry and and multiple child entries in a transaction.


Waiting for reply.


Thanks & Regards
Satheesh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 10:28 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
Please explain "not working". Anyway, if Your session is opened, it may choose to flush any changed objects it knows about at any time.

Gert


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.