-->
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: IDENTITY_INSERT ON / howto bind NHibernate and ADO sess/TAs
PostPosted: Wed Jun 15, 2005 3:08 am 
Newbie

Joined: Mon May 23, 2005 4:10 am
Posts: 9
hello!

i have a table "Fund" where the primary key is identity (autoassigned). now in a certain case i need to insert a record with a given Id. NHibernate's session.save(obj, id) would in theory allow this. however, as expected, the DBS (MSSQL2000) does not allow that.

so my question is: how can i insert a record with a predefined id?


one solution could be to use an ADO session, submit a IDENTITY_INSERT ON and OFF before and after the NHibernate session.save(obj, id) and somehow "bind" that session to the ADO session. this is where i can't figure it out. here's a part of my code (removed unimportant parts):

Code:
Protected Function addWithGivenPK(ByVal fund As FundDTO, ByVal id As Long) As FundDTO Implements IFundDAO.addWithGivenPK
   'Dim tx As NHibernate.ITransaction
   Dim tx2 As SqlTransaction
   Dim conn As SqlClient.SqlConnection = New SqlConnection(myConnectionString)
   Dim ret As Integer
   Dim cmdIdentityInsertOn As SqlClient.SqlCommand = New SqlClient.SqlCommand("SET IDENTITY_INSERT Fund ON; ", conn)
   'Dim cmdMiddle As SqlClient.SqlCommand = New SqlClient.SqlCommand("INSERT INTO Fund (FundId, Name) VALUES (101, 'test'); ", conn)
   Dim cmdIdentityInsertOff As IDbCommand = New SqlClient.SqlCommand("SET IDENTITY_INSERT Fund OFF", conn)

   conn.Open()
   Dim session2 As ISession = getSession(conn)

   tx2 = conn.BeginTransaction
   cmdIdentityInsertOn.Transaction = tx2
   'cmdMiddle.Transaction = tx2
   cmdIdentityInsertOff.Transaction = tx2

   ret = cmdIdentityInsertOn.ExecuteNonQuery()
   session2.Save(fund, id)
   'ret = cmdMiddle.ExecuteNonQuery()
   ret = cmdIdentityInsertOff.ExecuteNonQuery()

   tx2.Commit()
   conn.Close()
   Return fund
[ ... ]

please note: the cmdMiddle part was used to test if the id-insert command worked and it did works.
please note 2: no exception thrown here...
any hints on how to get the nhibernate use the same session or maybe even a better (nhibernate-integrated) solution?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 3:49 am 
Newbie

Joined: Mon May 23, 2005 4:10 am
Posts: 9
i tried two other ways and failed with all of them.

for the moment i'm using a cheap workaround, (create dummy fund with ADO, then retrieve it with a query). but this is a cheap hack and lead to potential failure (the ADO transaction must be committed, even though we don't know yet whether the nhibernate part worked or not).

so i would much prefer a "clean", nhibernate-based approach.


would it be possible to submit a non-"select" session.createquery? could a command be implemented with which you can enforce nhibernate to send the query to the DBMS without parsing it itself (and thereby allowing any command at all)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 11:31 am 
Newbie

Joined: Mon May 23, 2005 4:10 am
Posts: 9
sorry for coming up with this again... but am i the only one who's facing this issue?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 3:16 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Just use Session.Connection to issue any command you need at any time.


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.