-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Tue Apr 24, 2007 7:37 am 
Newbie

Joined: Tue Apr 24, 2007 7:29 am
Posts: 2
I tried to save a parent with his childs but it doesn't work.

I get the following error:
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1


I can't find the problem.

Here is my code

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="field" default-lazy="false">
   <class name="Softelligent.SoftStock.StockDomain.Order, Softelligent.SoftStock.StockDomain" table="Orders">
      <id name="m_id" column="OrderId">
         <generator class="assigned"></generator>
      </id>

      <property name="m_orderNumber" column="OrderNumber" />

      <bag name="m_orderItems" generic="true" access="field" inverse="true" lazy="false" cascade="all-delete-orphan">
         <key column="OrderItemOrderId"/>
         <one-to-many class="Softelligent.SoftStock.StockDomain.OrderItem, Softelligent.SoftStock.StockDomain" />
      </bag>
</class>
</hibernate-mapping>



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="field" default-lazy="false">
   <class name="Softelligent.SoftStock.StockDomain.OrderItem, Softelligent.SoftStock.StockDomain" table="OrderItems">
      <id name="m_id" column="OrderItemId">
         <generator class="assigned"></generator>
      </id>
            
      <many-to-one name="m_order" column="OrderItemOrderId" class="Softelligent.SoftStock.StockDomain.Order, Softelligent.SoftStock.StockDomain" update="true" />
   </class>
</hibernate-mapping>


Can somebody help me??

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Unexpected row count: 0; expected: 1
PostPosted: Tue Apr 24, 2007 7:43 am 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
TheAlmighty wrote:
I tried to save a parent with his childs but it doesn't work.

I get the following error:
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1


I can't find the problem.

Here is my code


Can somebody help me??

Thanks in advance.

FWIW, you haven't posted any of your code (C#) stuff that will tell us what's going on.

Also, I am having the same issue currently -- though mine happens because I wiped the development DB and went to a production DB that was clean. I'm sure that's not the only reason mine happens, as the code looks a bit shoddy.

I haven't yet found a solution -- I'm refactoring my code and cleaning everything up (probably as it should be), and am hoping that I'll fix the mistake there.

If anyone from the Nhibernate team has a good basis for when and why this error happens, that would help immeasurably. Right now it happens to me when I try to save a new object to the DB.

Try Googling for 'Unexpected Row Count 0', that exception phrase is almost solely used for Nhibernate. :-)

OT, but I'm also going to a talk tonight on db4o, anyone else ever use it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 7:54 am 
Newbie

Joined: Tue Apr 24, 2007 7:29 am
Posts: 2
Here's my c# code

Code:
foreach (OrderItem orderItem in m_orderItems) {
      m_order.AddOrderItem(orderItem);
}
m_orderService.Insert(m_order);


I'll try to find something with google, but I'm allready searching for 2 days.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 5:42 am 
Newbie

Joined: Wed Apr 25, 2007 6:43 pm
Posts: 1
TheAlmighty wrote:
Here's my c# code

Code:
foreach (OrderItem orderItem in m_orderItems) {
      m_order.AddOrderItem(orderItem);
}
m_orderService.Insert(m_order);


I'll try to find something with google, but I'm allready searching for 2 days.

Thanks.


Hi,

I had the same problem.

You use the assigned generator, and as is mentioned in nhibernate documentation :

"Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate()
method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling
either the Save() or Update() method of the ISession."

I suppose you want to save your objects using parent object (in your situation : order.Save()). To check if nHibernate must perform an Update or a Save, it test the value of the Id, If is null perform Save otherwise Update. In your code, Id is always not null because is assigned before saving it then NHibernate perform always an update, but there is no row to update, this is the explanation of the error message : Unexpected row count: 0; expected: 1

To correct this, use generator native :
<generator class="native"/>
instead of assigned.

P.S. Sorry for my english, I am french and my english is bad.

Fabien


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 12:16 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We are getting this error when we delete a child from a collection. We also use assigned IDs, but I don't see how that would affect deletes.

Our collections are mapped as bags with order-by set to a unique index column and cascade set to all-delete-orphan. In our tests we have two items in the collection. In each test, the item is removed from the collection and then the session is flushed; the cascade triggers the implicit delete. When we remove the last item in the collection and flush, it works fine, but when we remove the first item and flush, we get the exception. In both cases, the item is still deleted from the database, but the exception messes us up ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 8:20 am 
Regular
Regular

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

Can you provide simple test case for this problem?

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 2:39 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I haven't looked at this in a while, and getting back to it, I haven't figured out how to reproduce it reliably. If I can figure that out, I'll let you know ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 6:35 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Nels_P_Olsen wrote:
I haven't looked at this in a while, and getting back to it, I haven't figured out how to reproduce it reliably. If I can figure that out, I'll let you know ...


Is the error you are getting:
Unexpected row count: 0; expected: 1
or:
Unexpected row count: -1; expected: 1
?

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 6:46 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
The exception I get says
Code:
Unexpected row count: 0; expected: 1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 8:08 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
I had a problem like this not too long ago but forgot the fix. I know I had a few problems at the time, of which I remember...

One relating to using session.Transaction to check transaction state after commits or rollbacks (session.Transaction returns a new, inactive transaction after commit or rollback).

The other was related to either bad mappings in a table-per-subclass mapping or a one-to-many, I can't remember which.

I've got pretty good coverage on my data acess so I was able to isolate the issue and stamp it out. Unfortunately I can't recall what caused this issue in particular.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 7:23 am 
Newbie

Joined: Wed Sep 05, 2007 12:22 pm
Posts: 8
We also had this issue within a couple sections of code;

Our first problem was due to the composite key in the mapping file being incorrect.

The second issue was due to not creating a bi-directonal mapping and marking the inverse flag.

Third issue was due to an incorrect initialisation of the primary key.

HTH

Russ


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 5:13 am 
Newbie

Joined: Wed Jun 07, 2006 3:15 am
Posts: 14
I had that issue myself with a one-to-many relationship and cascade saves/updates.

In my case the problem was that the id (Int32) of the child was wrongly mapped with the attribute

unsaved-value="null"

instead of

unsaved-value="0"

In any case, try to search the log files the log4net generates when in debug mode. This is how I solved mine problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 1:28 pm 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
Hello,

so if we go back to the post of ibaf31 (Fabien)... How do save more objects (parent - childs) with one line of code (session.Save(parent)) if Id is assigned? I want Id to be assigned!!!

can this be done or not?

tenx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 2:01 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
I'm a bit confused as to what the specific issue is here.

I question that the original poster was really supposed to use assigned ids. I'm wondering if maybe a sequence is being used which changes the id and then when he goes to delete the item it can't find the appropriate item since a sequence value in the database changed the primary key. When the item wasn't found it then threw the given exception.

As to how you handle assigned keys and save-or-update. You have to use a version property. If you add a version tag in your mapping file you can use the version to determine if it is a new item or not. If you are using an integer version then you could treat 0 as being a new item, anything greater than 0 would be an update.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 3:28 pm 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
Hy,

I had in mind the question of inserting (no deleting) of new objects.
Do I have to add a new column in db.table to support this version tag. I dont want that.

When I used identity class generator, NH knew both objects (parent and one child) were new and created two insert and one update sql statements and everythink was working fine.

Now I want to have same functionality with assigned class generator (I set Id in code after I call class's constructor), but I see that NH does not insert child object (because it thinks its not new object) but it wants to update it -> ergo: this thread's name error occurs.

Q: how can I define that all objects in one object graph are new (need insert sql statement).

I am new to NH, maybe these are basics ... Thanks very much for help.

br


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.