-->
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: Problems with Guid type
PostPosted: Sun Nov 18, 2007 2:37 pm 
Newbie

Joined: Sun Nov 18, 2007 2:07 pm
Posts: 2
Hello,

i am using NHibernate with a Firebird database. I have a strange behaviour with the Guid type. I am using the a Guid as primary key for my "Users" table in a test application. The realated database field is a "char" with lenght "36". So far everything works fine. I can add new rows and update rows. But if I try to delete an existing row I get an exception if the transaction is beeing comited.

Quote:
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1


The strange thing is, if i change the LogoID property in im Code to a string type, everything works fine. But this workaround is not a good alternative for me.

Hibernate version: Build 1.2.0.GA

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateTest.Persistence.User, NHibernateTest" table="&quot;Users&quot;">
    <id name="Id" column="&quot;LogonID&quot;" type="Guid" length="36">
      <generator class="assigned" />
    </id>
    <property name="UserName" column="&quot;UserName&quot;" type="String" length="40"/>
    <property name="Password" column="&quot;Password&quot;" type="String" length="20"/>
    <property name="EmailAddress" column="&quot;EmailAddress&quot;" type="String" length="255"/>
    <property name="LastLogon" column="&quot;LastLogon&quot;" type="DateTime"/>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
C#
Code:
            using (ITransaction transaction = session.BeginTransaction())
            {
                try
                {
                    Guid guid = new Guid(comboBoxGetUserById.SelectedItem.ToString());
                    User delUser = (User)session.Load(typeof(User), guid);

                    if (delUser != null)
                    {
                        session.Delete(delUser);
                    }

                    transaction.Commit();               
                }
                catch (Exception exception)
                {
                    transaction.Rollback();
                    MessageBox.Show(exception.Message);
                }
            }


Full stack trace of any exception that occurs:
Code:
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1
   bei NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
   bei NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
   bei NHibernate.Impl.ScheduledDeletion.Execute()
   bei NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   bei NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
   bei NHibernate.Impl.SessionImpl.Execute()
   bei NHibernate.Impl.SessionImpl.Flush()
   bei NHibernate.Transaction.AdoTransaction.Commit()
   bei NHibernateTest.FormMain.buttonDelete_Click(Object sender, EventArgs e) in E:\Projects\NHibernateTest\NHibernateTest\FormMain.cs:Zeile 140.


Name and version of the database you are using:
Firebird v 2.0.3


Thanks for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 18, 2007 4:41 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Run a trace and review the SQL which is being ran. That error occurs based on what happens at the database level. It sees that no rows are being deleted even though it executed a delete statement, and then throws an exception. I've never used FireBird so I can't really comment on the details of why that would happen. If your mapping file actually has the &quot; in them, then that seems weird, but again that could be normal for FireBird, I wouldn't know. It also may be a bug with the FireBird dialect. Post some more details after you turn on SQL logging.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 18, 2007 5:25 pm 
Newbie

Joined: Sun Nov 18, 2007 2:07 pm
Posts: 2
jchapman wrote:
Run a trace and review the SQL which is being ran. That error occurs based on what happens at the database level. It sees that no rows are being deleted even though it executed a delete statement, and then throws an exception. I've never used FireBird so I can't really comment on the details of why that would happen. If your mapping file actually has the &quot; in them, then that seems weird, but again that could be normal for FireBird, I wouldn't know. It also may be a bug with the FireBird dialect. Post some more details after you turn on SQL logging.


First of all tanks for your reply. Maybe you are right and somethink is wrong with the FireBird dialect. I turned the logging on. Here is the result:

Code:
2007-11-18 22:08:32,425 [10] DEBUG [NHibernate.Persister.Entity.AbstractEntityPersister] Deleting entity: [NHibernateTest.Persistence.User#2db0e09c-cda9-407c-8c04-d4d3d49664dd]
2007-11-18 22:08:32,441 [10] DEBUG [NHibernate.Impl.BatcherImpl] Opened new IDbCommand, open IDbCommands: 1
2007-11-18 22:08:32,441 [10] DEBUG [NHibernate.Impl.BatcherImpl] Building an IDbCommand object for the SqlString: DELETE FROM "Users" WHERE "LogonID" = ?
2007-11-18 22:08:32,441 [10] DEBUG [NHibernate.Type.GuidType] binding '2db0e09c-cda9-407c-8c04-d4d3d49664dd' to parameter: 0
2007-11-18 22:08:32,441 [10] DEBUG [NHibernate.SQL] DELETE FROM "Users" WHERE "LogonID" = @p0; @p0 = '2db0e09c-cda9-407c-8c04-d4d3d49664dd'
2007-11-18 22:08:32,456 [10] DEBUG [NHibernate.Impl.BatcherImpl] Closed IDbCommand, open IDbCommands: 0
2007-11-18 22:08:32,456 [10] ERROR [NHibernate.Impl.SessionImpl] could not synchronize database state with session
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1
   bei NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
   bei NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
   bei NHibernate.Impl.ScheduledDeletion.Execute()
   bei NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
   bei NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
   bei NHibernate.Impl.SessionImpl.Execute()


When I try to execute the Delete-statement manually it doesn't work. This has to be the problem. The main problem seems to be the assignment pattern in "WHERE "LogonID" = @p0; @p0 = '2db0e09c-cda9-407c-8c04-d4d3d49664dd'", the database engine doesn't understand this.

The correct statement would be:
Code:
DELETE FROM "Users" WHERE "LogonID" = '2db0e09c-cda9-407c-8c04-d4d3d49664dd'


Now I just have to find out how I can bring NHibernate to do this for me :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 18, 2007 7:19 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Again, I'm not FireBird expert. But I don't think your sql statement is actually what you want. The difference is that NHibernate is trying to run a parameterized query. The values you are pulling from the log is not exactly how it will be run when it reaches the database. Basically you can pass parameters to a SqlCommand in ADO.NET. NHibernate always shows the parameters in the manner you showed.

Do you know how to run parameterized statements in Firebird? I honestly don't, but I would have to assume it is possible.

Parameterized statements are superior to non-parameterized statements. First off, by using parameters ADO.NET will help guard against SQL injection by making sure that the parameters are safe and will not result in unwanted sql.

Secondly, at least for Sql Server, the database engine can cache the query plan for the provided statement. Therefore the next time the database receives the exact same statement (except with a different parameter value) the execution plan for the statement is already stored. The query can just be executed.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 10:51 am 
Newbie

Joined: Mon Jan 07, 2008 10:42 am
Posts: 1
http://jira.nhibernate.org/browse/NH-1000

Looks like this issue "hanged".

NHibernate creates records, but will have trouble selecting/updating records.

FB client treats Guid as char(16) octet.

I resolved this issue by slightly modifying FB client :)[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 10, 2008 5:55 am 
Newbie

Joined: Wed Sep 21, 2005 2:53 pm
Posts: 1
eys wrote:
http://jira.nhibernate.org/browse/NH-1000

Looks like this issue "hanged".

NHibernate creates records, but will have trouble selecting/updating records.

FB client treats Guid as char(16) octet.

I resolved this issue by slightly modifying FB client :)[/url]


Can you tell us your modification?


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.