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 Aug 18, 2008 7:19 am 
Newbie

Joined: Mon Aug 18, 2008 7:02 am
Posts: 6
Hi I'm new to Nhibernate, I'm tring to use it with oracle & visual studio 2008. For Select,Update & Delete it is working fine but while saving(Insert) it shows me following error :- Unexpected row count: 0; expected: 1.

My code is as follows:

class file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using NHibernate;

/// <summary>
/// Summary description for TBL_EMPLOYEE
/// </summary>
namespace NHEMPLOYEE
{

public partial class TBL_EMPLOYEE
{
public static NHEMPLOYEE.TBL_EMPLOYEE GetEmployee(string EMP_CODE)
{
ISession session = NHibernateHelper.GetCurrentSession();
NHEMPLOYEE.TBL_EMPLOYEE employee = (NHEMPLOYEE.TBL_EMPLOYEE)session.Load(typeof(NHEMPLOYEE.TBL_EMPLOYEE), EMP_CODE);
return employee;
}
public static List<NHEMPLOYEE.TBL_EMPLOYEE> getEmployees()
{
List<NHEMPLOYEE.TBL_EMPLOYEE> employee = new List<NHEMPLOYEE.TBL_EMPLOYEE>();
// Open the session
ISession session = NHibernateHelper.GetCurrentSession();
// Load the customers from the database
session.CreateCriteria(typeof(NHEMPLOYEE.TBL_EMPLOYEE)).List(employee);
return employee;
}
public static void saveOrUpdateEmployee(NHEMPLOYEE.TBL_EMPLOYEE employee)
{
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction transaction = null;

try
{
// Start transaction
transaction = session.BeginTransaction();
// Save or update (saves if the id is null, otherwise it updates)
session.SaveOrUpdate(employee);
//session.Save(employee);
// Commit transaction
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}

}
public static void deleteEmployee(NHEMPLOYEE.TBL_EMPLOYEE employee)
{
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction transaction = null;

try
{
// Start transaction
transaction = session.BeginTransaction();

// Delete
session.Delete(employee);

// Commit transaction
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
}
}

mapping file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHEMPLOYEE.TBL_EMPLOYEE, __Code" table="TBL_EMPLOYEE">
<id name="EMP_CODE" column="EMP_CODE" type="String" >
<generator class="assigned"/>
</id>
<property name="EMP_NAME" column="EMP_NAME" type="String" />
<property name="EMP_DEPTNAME" column="EMP_DEPTNAME" type="String"/>
<property name="EMP_EMAILID" column="EMP_EMAILID" type="String" not-null="true"/>
<property name="EMP_PASSWORD" column="EMP_PASSWORD" type="String"/>
</class>
</hibernate-mapping>

web.config

<?xml version="1.0"?>

<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<nhibernate>
<add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect"
value="NHibernate.Dialect.Oracle9Dialect" />
<add key="hibernate.connection.driver_class"
value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.connection.connection_string"
value="Data Source=ORCL_SYSTEM6;User Id=DTS;Password=reliance;persist security info=false;">
</add>
<!--value="Provider=MSDAORA;Data Source=ORCL_SYSTEM6;User Id=DTS;Password=reliance;persist security info=false;"/>-->
<add key="cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache"/>
<add key="cache.use_query_cache" value="True"/>
<add key="cache.expiration" value="120"/>
<add key="query.substitutions" value="True=1;False=0"/>

<add key="hibernate.connection.isolation" value="ReadCommitted" />
<add key="hibernate.show_sql" value="True" />
</nhibernate><appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 9:06 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
From the documentation:
Quote:
Assigned Identifiers

If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

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.


So you have to either choose Save() or Update() yourself or use another id generator.


Top
 Profile  
 
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Mon Aug 18, 2008 10:58 am 
Newbie

Joined: Mon Aug 18, 2008 7:02 am
Posts: 6
thanks for quick reply ....
I have already checked both methods. I'm binding ObjectDataSource control to grid.For InsertMethod attribute I'm specifing "saveEmployee()" to call session.Save(employee); & for UpdateMethod session.Update(employee); but it is not calling the saveEmployee(). I also specified the <generator class="assigned" />
in hbn.xml file. I wanted to hightlight one point the same code is working fine for MS sql 2005 but giving problem for oracle & Mysql.


[quote="cremor"]From the documentation:
[quote]Assigned Identifiers

If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

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.[/quote]

So you have to either choose Save() or Update() yourself or use another id generator.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 19, 2008 2:19 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
So you are not using the saveOrUpdateEmployee() method you posted but calling session.Save() or session.Update() yourself? Ok, that should work then.

What do you mean with "it is not calling the saveEmployee()"? Your method gets never called by the ObjectDataSource? Then I'd double check if the datasource is set up correctly as well as how you call the update method (either CommandName="Update" or by code).


Top
 Profile  
 
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Tue Aug 19, 2008 3:01 am 
Newbie

Joined: Mon Aug 18, 2008 7:02 am
Posts: 6
Hi,
should I send u detailed code?


[quote="cremor"]So you are [i]not[/i] using the saveOrUpdateEmployee() method you posted but calling session.Save() or session.Update() yourself? Ok, that should work then.

What do you mean with "it is not calling the saveEmployee()"? Your method gets never called by the ObjectDataSource? Then I'd double check if the datasource is set up correctly as well as how you call the update method (either CommandName="Update" or by code).[/quote]


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.