-->
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.  [ 8 posts ] 
Author Message
 Post subject: Only last command executed after a flush?
PostPosted: Thu Jul 27, 2006 3:58 am 
Newbie

Joined: Tue Mar 07, 2006 4:56 am
Posts: 17
Hello together...

I got a problem with hibernate and flushing objects. For example, I create a number of new objects and use the "Save" method of the current Session to indicate to hibernate, that those are new instances. When I'm done with my loop, I use a flush() to write the data to the database. However, only the last created object becomes persistent.

The same happens, if I first delete an object from the database (using delete on the session) and then re-create the object. When I do a flush then, I get a key violation, because the object was never deleted. Shouldn't the flush remember that I wanted to delete the object and do that first, prior to do the insert?

Does anyone have an idea how to fix this behaviour? It's kinda pointless if I have to do a flush after every command.

Kind regards, Marco...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 4:37 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
Just a tought: have you tried to open a transaction before entering the loop?
Can you post the code, please?

Cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 4:54 am 
Newbie

Joined: Tue Mar 07, 2006 4:56 am
Posts: 17
Hi Radu,

no, I'm not using an transaction. Here is the code I use:

[TestFixtureSetUp]
public void Init()
{
m_UserProvider = PlatformProvider.Get.User;
Finalize();
ISession DBSession = SCPApp.GetDBSession();
try
{

for (int i = 0; i < 50; i++)
{
m_UserProvider.CreateNewUser("unittest" + i.ToString("D2"), "password" + i.ToString(), DBSession);
}
}
finally
{
DBSession.Flush();
DBSession.Close();
}
}

It works, if I do the flush directly after the CreateNewUser. Here is also the code for "CreateNewUser":

public IUser CreateNewUser(string LoginName, string Password, ISession DBSession )
{
IUser Result = new User();

Result.LoginName = LoginName;
Result.Password = Password;
Result.ACL.OwnerID = Result.ObjectID;
AssignDBSession(Result, DBSession);
Result.Save();
return Result;
}

Regards, Marco...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 5:09 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
are you sure that your 50 objects have 50 different id?

radu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 5:57 am 
Newbie

Joined: Tue Mar 07, 2006 4:56 am
Posts: 17
Hi Radu,

yes, I'm sure. I use a Guid as ID and assign it during the save process myself (though I guess it get's overwritten by the comb ID generator later on).

Regards, Marco...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 6:05 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
well, just to check the behaviour, enclose the loop in a transaction. In fact Hibernate best practices require to always use a transaction. Usually a simple save involves more than one sql statements.

Radu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 7:16 am 
Newbie

Joined: Tue Mar 07, 2006 4:56 am
Posts: 17
Ok, I tried putting the whole thing into a transaction and commit it in the end. No difference. I can even see NHibernate "saving" the objects, including the object id. But the flush (triggered through the commit) does only save the last entry (unittest49). Any other ideas?

Kind regards, Marco


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 8:43 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
post the full code and mapping file

radu


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