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.  [ 3 posts ] 
Author Message
 Post subject: Re-SELECTing an object created previously in same session
PostPosted: Thu Jul 05, 2007 6:20 am 
Newbie

Joined: Tue Jun 12, 2007 8:25 am
Posts: 3
Hi,

I need to insert or update a number of price codes in the database. I would prefer to do this in one session for performance reasons but I am running into trouble when a price code appears twice in the input feed, either with exactly the same info or with changed info. If the info is the same I would like to just ignore the input, if the info is different I want to update the record. Point is, the record might have been created earlier in the session, so my SELECT statement returns that there is no record yet. So I create a new row, try to Save it and get a UniqueKeyViolation exception (or similar).

So right now I am Flushing the session after each SaveOrUpdateCopy (I use *Copy to prevent yet another related issue). I would most definately like to prevent having to Flush every time.

Is there any way I can do my SELECT statement (or a similar construct) on the session itself, so I can retrieve a record that was cerated earlier in the session? I've seen the Get method but that does not allow me to specify a query.

My code is below:

Code:
for ( ; current < priceCodes.Count; current++ )
{
  XmlNode priceCode = priceCodes[current];

  // The following 4 values uniquely identify a price code
  string resourceCode = priceCode["RsrcCode"].InnerText;
  string countryCode = priceCode["CtyCodeRel"].InnerText;
  string dspCode = priceCode["CpyIDSP"].InnerText;
  DateTime effectiveDate = DateTime.ParseExact( priceCode["EffectiveDate"].InnerText, "yyyy-MM-dd", null );

  PriceCodeService pcs = new PriceCodeService();
  ReleasePriceCode pc = pcs.GetPriceCode( resourceCode, countryCode,
                                           dspCode, effectiveDate );

  if ( pc == null )
  {
    Log.DebugFormat( "Price Code ({0}:{1}:{2}) doesn't exist, need to create",
      resourceCode, countryCode, dspCode );
    pc = new ReleasePriceCode();
   }

  pc.ResourceCode = resourceCode;
  pc.CountryCode = countryCode;
  pc.DigitalSalesPartnerCode = dspCode;
  pc.EffectiveDate = effectiveDate;
  pc.PriceCode = priceCode["PriceCode"].InnerText;

  // *Copy is required to enable updating an existing record that was previously updated
  session.SaveOrUpdateCopy( pc );
  // Flush is required for GetPriceCode (above) to return a previously created price code
  session.Flush();
}


I hope I've made my problem clear enough and that someone can provide some assistance.

Thanks!

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 11:05 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi ErikB!

AFAIK, currently there is no way to query in-memory data with NHibernate. I suppose things will be much different when LINQ will be released...
NHibernate now make all queries by converting HQL оr IQuery syntax to SQL and executing it on backend DBMS - so if there is no record in DB, there will be no result. Hovever default setting for Session.FlushMode lead to automatic flush before any query is executed...

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 11:32 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The reason why you have to perform a manual flush after each insert for the duplicate checks to work is because you are performing the query in what appears to be a different ISession entirely, underneath the call to PriceCodeService.GetPriceCode().

I would add that in your bulk-load scenario, it seems like it would be much more performant for you to prune duplicates in memory, perhaps using a HashedSet.


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