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.  [ 5 posts ] 
Author Message
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Mon Feb 09, 2009 3:26 am 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
I hope you help me with this issue, since i'm really stuck with this problem. I think it could be related with Id (is native). By the way, i have not been able to configure log4net properly and exception trace is not offering too much information.

Any help will be really appreciated

Hibernate version:
2.0.1.4000
Mapping documents:

Code:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="PersistanceStore.Tests"
                   assembly="PersistanceStore.Tests"
                   default-lazy="false">

  <!-- Mappings for class 'SwitchingOrderAdaptor' -->
  <class name="SwitchingOrderAdaptor" table="RX_SWITCHING_ORDERS">
    <!-- Identity mapping -->
    <id name="Id">
      <column name="Id"/>
      <generator class="native" />
    </id>
    <!-- Simple mappings -->
    <property name="Name" column="Name"/>
    <one-to-one name="SwitchingStepGroupAdaptor" class="SwitchingStepGroupAdaptor" cascade="all-delete-orphan"/>
  </class>
</hibernate-mapping>



<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="PersistanceStore.Tests"
                   assembly="PersistanceStore.Tests"
                   default-lazy="false">

  <!-- Mappings for class 'SwitchingStepGroupAdaptor' -->
  <class name="SwitchingStepGroupAdaptor" table="RX_SWITCHING_STEP_GROUPS">
    <!-- Identity mapping -->
    <id name="Id">
      <column name="Id"/>
      <generator class="native"/>
    </id>
    <!-- Simple mappings -->
    <property name="GroupName" column="GroupName"/>
    <property name="StepCount" column="StepCount"/>
    <!--</component>-->
    <bag name="SwitchingStepAdaptors" table="RX_SWITCHING_STEPS" cascade="all">
      <key column="SwitchingStepGroupId" />
      <one-to-many class="SwitchingStepAdaptor" />
    </bag>
    <many-to-one name="SwitchingOrderAdaptor" column="SwitchingOrderId" cascade="save-update"/>
  </class>
</hibernate-mapping>


<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="PersistanceStore.Tests"
                   assembly="PersistanceStore.Tests"
                   default-lazy="false">

  <!-- Mappings for class 'SwitchingStepGroupAdaptor' -->
  <class name="SwitchingStepAdaptor" table="RX_SWITCHING_STEPS">
    <!-- Identity mapping -->
    <id name="Id">
      <column name="Id"/>
      <generator class="native"/>
    </id>
    <!-- Simple mappings -->
    <property name="StepCount"/>
    <!--<property name="CurrentTaskState"/>
    <property name="Description"/>
    <property name="Name"/>
    <property name="TaskStatus"/>
    <property name="Title"/>-->
    <many-to-one name="SwitchingStepGroupAdaptor" column="SwitchingStepGroupId" cascade ="save-update"/>
  </class>
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

Code:

            using (ISession _session = _sessionFactory.OpenSession())
            {
                using (_session.BeginTransaction())
                {
                    _session.Save(entity);
                    _session.Transaction.Commit();
                }   
            }


Full stack trace of any exception that occurs:
Code:
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1
   at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
   at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
   at NHibernate.Action.EntityUpdateAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteActions()
   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()
   at NHibernate.Transaction.AdoTransaction.Commit()
   at Miner.Responder.PersistanceStore.RepositoryNHibernate`1.Save(T entity) in D:\switchOrders\source\Miner.Responder\Miner.Responder.PersistanceStore\RepositoryNHibernate.cs:line 82
   at Miner.Responder.PersistanceStore.RepositoryServiceLocator`1.SaveInRepositories(T objectToSave) in D:\switchOrders\source\Miner.Responder\Miner.Responder.PersistanceStore\RepositoryServiceLocator.cs:line 41
   at PersistanceStore.Tests.TestRepositoryServiceLocator.SwitchingOrder() in D:\switchOrders\source\Miner.Responder\PersistanceStore.Tests\TestRepositoryServiceLocator.cs:line 358


Name and version of the database you are using:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=(local)\SQLExpress;Initial Catalog=StoragePersistanceTest;Integrated Security=True;Pooling=False
</property>
</session-factory>
</hibernate-configuration>

The generated SQL (show_sql=true):





CREATE TABLE [dbo].[RX_SWITCHING_ORDERS](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_RX_SWITCHING_ORDERS] PRIMARY KEY CLUSTERED
([Id] ASC)
WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]




CREATE TABLE [dbo].[RX_SWITCHING_STEP_GROUPS](
[Id] [int] IDENTITY(1,1) NOT NULL,
[GroupName] [varchar](50) NULL,
[StepCount] [int] NULL,
[SwitchingStepGroupId] [int] NULL,
[SwitchingOrderId] [int] NOT NULL,
CONSTRAINT [PK_RX_SWITCHING_STEP_GROUPS] PRIMARY KEY CLUSTERED
([Id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]


CREATE TABLE [dbo].[RX_SWITCHING_STEPS](
[Id] [int] IDENTITY(1,1) NOT NULL,
[StepCount] [int] NULL,
[CurrentTaskState] [int] NULL,
[Description] [varchar](250) NULL,
[Name] [varchar](50) NULL,
[TaskStatus] [int] NULL,
[Title] [varchar](50) NULL,
[SwitchingStepGroupId] [int] NOT NULL,
CONSTRAINT [PK_RX_SWITCHING_STEPS] PRIMARY KEY CLUSTERED
([Id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


ALTER TABLE [dbo].[RX_SWITCHING_STEP_GROUPS] WITH CHECK ADD CONSTRAINT [FK_RX_SWITCHING_STEP_GROUPS_RX_SWITCHING_ORDERS1] FOREIGN KEY([SwitchingOrderId])
REFERENCES [dbo].[RX_SWITCHING_ORDERS] ([Id])


ALTER TABLE [dbo].[RX_SWITCHING_STEP_GROUPS] CHECK CONSTRAINT [FK_RX_SWITCHING_STEP_GROUPS_RX_SWITCHING_ORDERS1]


ALTER TABLE [dbo].[RX_SWITCHING_STEPS] WITH CHECK ADD CONSTRAINT [FK_RX_SWITCHING_STEPS_RX_SWITCHING_STEP_GROUPS1] FOREIGN KEY([SwitchingStepGroupId])
REFERENCES [dbo].[RX_SWITCHING_STEP_GROUPS] ([Id])


ALTER TABLE [dbo].[RX_SWITCHING_STEPS] CHECK CONSTRAINT [FK_RX_SWITCHING_STEPS_RX_SWITCHING_STEP_GROUPS1]


Debug level Hibernate log excerpt:



PS:I've edited so code can be easier read


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2009 4:30 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
A new update (in case someone could help me)

Basically, I've got following structure:


GrandParent--->Parent--->Children(Grandsons)


Where:
GrandParent is SwitchingOrderAdaptor
Parent is SwitchingStepGroup
Children is SwitchingStep


I'm observice that GrandParent saving insertion implies Parent insertion, but it doesn't imply Children insertion.

After all this changes, Parent are being updated and tries to update Children, so it fails and here is where "Unexpected count" appears.

I think problem could be fixed if i was able to insert Children when i insert GrandParent. My current approach is setting it in Parent.hbm.xml, since my understanding is using cascade will create (when Parent was created) children. Since i'm saving GrandParent object, may be my assumption is wrong. Is it possible to save Children by configuring GrandParent.hbm.xml?

Here are the SQL traced into output:

Code:
NHibernate: INSERT INTO RX_SWITCHING_ORDERS (Name) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'SWO1'

NHibernate: INSERT INTO RX_SWITCHING_STEP_GROUPS (GroupName, StepCount, SwitchingOrderId) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY(); @p0 = '', @p1 = '69', @p2 = '66'

NHibernate: UPDATE RX_SWITCHING_STEPS SET SwitchingStepGroupId = @p0 WHERE Id = @p1; @p0 = '60', @p1 = '1'


Top
 Profile  
 
 Post subject: Re: Unexpected row count: 0; expected: 1
PostPosted: Mon Feb 09, 2009 9:27 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
Solved.

I've got a rule. Probably is at any place in documentation, but iwas not able to find it.

Well... you know it's needed to have an empty constructor on those classes you want to be mapped. I was initializing lists inside those constructors. So, it's really important you don't do this kind of operations.

In adition, i've seen several posts on the web preventing assigning Id's at any time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 4:36 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The problem is probably not that the constructor has to be empty (it definetely has not), but that the lists you're instantiating are something hibernate does not like. Can you post one of those constructors and the according properties that are mapped ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 1:53 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
wolli wrote:
The problem is probably not that the constructor has to be empty (it definetely has not), but that the lists you're instantiating are something hibernate does not like. Can you post one of those constructors and the according properties that are mapped ?


This is what i was doing. Sorry for being delayed on answer but i needed to access Source Control.

This was the class which was driving me crazy as it was. Notice that i've moved list initialization from constructor into paremterized constructor:

Code:
    class SwitchingStepGroupAdaptor
    {
        public SwitchingStepGroupAdaptor(){SwitchingOrders = new List<SwitchingOrderAdaptor>();}
        public SwitchingStepGroupAdaptor(SwitchingStepGroup switchingStepGroup):this()
        {
            GroupName = switchingStepGroup.GroupName;
            Id = switchingStepGroup.Id;
            StepCount = switchingStepGroup.StepCount;
        }

        public string GroupName { get; set; }
        public int Id { get; set; }
        public int StepCount { get; set; }
        public IList<SwitchingOrderAdaptor> SwitchingOrders { get; set; }
    }


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