-->
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: Do I have my concepts wrong?
PostPosted: Thu Jan 10, 2008 5:53 am 
Newbie

Joined: Thu Jan 10, 2008 3:50 am
Posts: 4
[Sorry for posting in wrong forum before]

Hi,

I have a couple of problems and I've been banging my head against the wall. I need to do some simple operations that I think any business application can have...nothing out of the ordinary.

When I create a new customer, I have a custom method to obtain a numerator (it's not the ID and I don't want to use generator). Is it not possible to just execute a statement? Soemthing like "update x set y=y+1"? Forget even returning the result. Can't I just execute a direct SQL statement? I just don't see myself having to create a numerator class, the class mapping and do things like:

Numerator numerator = new Numerator();
int value = numerator.GetNextNumerator();
session.Update(numerator);

where GetNextNumerator gets a ID and then increments it. It's just an overkill.

My other problem is with transactions. If I have a sequence generator in the ID, TransactionScope fails with cannot open data connection. I have to use Begin/End Transaction.

Also, even if I rollback my transaction, my generator in Firebird is still updated.

I'm really close to throwing in the towel with NH and I don't want to :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 1:02 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Just executing a SQL query can be done using the CreateSQLQuery method on the ISession.

I don't use this for UPDATES/INSErTS so im not quit sure how to handle returntypes (as i recall nHibernate demand an returntype) so my guess would be to use an int and perhabs it will hold the rows affected, try it out. Something like this:

Code:
object o = mySession.CreateSQLQuery("UPDATE x set y = y + 1")
                                .AddScalar("", NHibernate.Type.TypeFactory-Basic("int"))
                                .UniqueResult();



Perhabs some of the cleverer heads in here can clarify why a type is neccesary.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 3:11 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Those are required I think because Create SQL Query is intended for, well, queries, not NonQueries.

You could also go a little deeper and grab the connection from the session (or directly from the factory ConnectionProvider) and build your own IDbCommand:

Code:
//sketch
IDbConnection conn = factory.ConnectionProvider.GetConnection();
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE x set y = y + 1";
cmd.Connection = conn;
//enlist to transaction if desired

cmd.ExecuteNonQuery();
conn.Close();


As far as the sequence being incremented even after rollback: I can't speak with authority in re Firebird, but I think that might be a DB level "issue". The same thing happens in SQL with identity columns: rollbacks produce "skipped" identifiers. But really, this should never be a problem, since the identifier (and even the relationships between identifiers) is not intended to carry any meaning.


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.