fluent nhibernate-1.1 stored procedure question

Mark Gellings's Avatar

Mark Gellings

01 Sep, 2010 06:36 PM via web

Hi I'm executing a stored procedure with NHibernate using Fluent NHibernate and having a couple problems I'd like for someone to help me with. :) I can only find limited documentation on using stored procedures with Fluent NHibernate on the web and not enough to let me further troubleshoot the problems I'm having. My mapping class, unit test and an excerpt from the log file is below for more information.

The questions I have are:

  • should Fluent NHibernate be placing the property marked as the Id as the last parameter of the proc? The WCCustomerId property should be the Id for this entity.
  • when I get the proc to execute (by setting ErrorMsg as the Id) it throws back an NHibernate.StaleStateException "Unexpected row count: -1; expected: 1" Is this expected behavior if the proc is returning -1? We've programmed the proc to set the ErrorMsg and ErrorCode parameters upon an error in the procedure. Will NHIbernate set those properties on the entity to the output parameter value by default?

Thanks. I'll appreciate any answers I get as received no response when posting on stackoverflow.com.

public class ContactMap: ClassMap { public ContactMap() {

   SqlInsert(
        "begin wc.pkg_crm_load.sp_load_crm_contact(:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18, :p19, :p20, :p21, :p22, :p23 ); end; ");
   Id(x => x.WCCustomerID);

    /* commented out
        WCCustomerID = "130", 
        WCContactID = "990000333", 
        Street1 = "Magazine Group", 
        Street2 = "110 Fifth Avenue", 
        Street3 = null, 
        City = "New York", 
        State = "NY", 
        Country = "USA", 
        Zip = "10011", 
        Email = null, 
        Fax = null, 
        FirstName = "Mike", 
        JobTitle = "Test", 
        LastName = "Montemurro", 
        MobilePhone = null, 
        Status = "1", 
        BusinessPhone = "555-5555", 
        HomePhone = null,
        QwikLink = 0, 
        Billing = 1, 
        FreightBilling = 0, 
        ModifiedBy = "Mark Gellings" */



    Map(x => x.WCContactID);
    Map(x => x.Street1, "as_address_1");
    Map(x => x.Street2, "as_address_2");
    Map(x => x.Street3, "as_address_3");
    Map(x => x.City, "as_city");
    Map(x => x.State, "as_state");
    Map(x => x.Zip, "as_zip");
    Map(x => x.Country, "as_country");
    Map(x => x.FirstName, "as_first_name");
    Map(x => x.LastName, "as_last_name");
    Map(x => x.Position, "as_position");
    Map(x => x.BusinessPhone, "as_business_phone");
    Map(x => x.MobilePhone, "as_mobile_phone");
    Map(x => x.HomePhone, "as_home_phone");
    Map(x => x.Fax, "as_fax");
    Map(x => x.Email, "as_email_address");
    Map(x => x.Billing, "an_billing_contact_flag");
    Map(x => x.FreightBilling, "an_freight_bill_contact_flag");
    Map(x => x.QwikLink, "an_qwiklink_flag");
    Map(x => x.Status, "an_status_flag");
    Map(x => x.ModifiedBy, "as_row_updated_by");
    Map(x => x.ErrorCode, "an_error_code");
    Map(x => x.ErrorMsg, "as_error_message");

}

[Test] public void can_call_the_contact_stored_procedure() { XmlConfigurator.Configure();

    var contact = new ContactDto() { 
        WCCustomerID = "130", 
        WCContactID = "990000333", 
        Street1 = "Magazine Group", 
        Street2 = "110 Fifth Avenue", 
        Street3 = null, 
        City = "New York", 
        State = "NY", 
        Country = "USA", 
        Zip = "10011", 
        Email = null, 
        Fax = null, 
        FirstName = "Mike", 
        JobTitle = "Test", 
        LastName = "Montemurro", 
        MobilePhone = null, 
        Status = 1, 
        BusinessPhone = "555-5555", 
        HomePhone = null,
        QwikLink = 0, 
        Billing = 1, 
        FreightBilling = 0, 
        ModifiedBy = "Mark Gellings",
       ErrorMsg = "null"}
    ;

     using (var trans = _session.BeginTransaction())
            {

                var dao = new StatefulDao(new CRMReplicationServiceUnitOfWork());

                _session.Save(contact);
                // trigger unit of work to be flushed in a transaction
                trans.Commit();
            }
}

2010-08-30 13:20:52,517 [7] [DEBUG] NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [Quad.MasterDatabase.CRMReplicationService.DataTransfer.ContactDto#130] 2010-08-30 13:20:52,532 [7] [DEBUG] NHibernate.Type.StringType - binding '990000333' to parameter: 0 2010-08-30 13:20:52,532 [7] [DEBUG] NHibernate.Type.StringType - binding 'Magazine Group' to parameter: 1 2010-08-30 13:20:52,548 [7] [DEBUG] NHibernate.Type.StringType - binding '110 Fifth Avenue' to parameter: 2 2010-08-30 13:20:52,564 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 3 2010-08-30 13:20:52,564 [7] [DEBUG] NHibernate.Type.StringType - binding 'New York' to parameter: 4 2010-08-30 13:20:52,579 [7] [DEBUG] NHibernate.Type.StringType - binding 'NY' to parameter: 5 2010-08-30 13:20:52,579 [7] [DEBUG] NHibernate.Type.StringType - binding '10011' to parameter: 6 2010-08-30 13:20:52,595 [7] [DEBUG] NHibernate.Type.StringType - binding 'USA' to parameter: 7 2010-08-30 13:20:52,595 [7] [DEBUG] NHibernate.Type.StringType - binding 'Mike' to parameter: 8 2010-08-30 13:20:52,610 [7] [DEBUG] NHibernate.Type.StringType - binding 'Montemurro' to parameter: 9 2010-08-30 13:20:52,626 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 10 2010-08-30 13:20:52,626 [7] [DEBUG] NHibernate.Type.StringType - binding '555-5555' to parameter: 11 2010-08-30 13:20:52,642 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 12 2010-08-30 13:20:52,642 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 13 2010-08-30 13:20:52,657 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 14 2010-08-30 13:20:52,657 [7] [DEBUG] NHibernate.Type.StringType - binding null to parameter: 15 2010-08-30 13:20:52,673 [7] [DEBUG] NHibernate.Type.Int32Type - binding '1' to parameter: 16 2010-08-30 13:20:52,673 [7] [DEBUG] NHibernate.Type.Int32Type - binding '0' to parameter: 17 2010-08-30 13:20:52,689 [7] [DEBUG] NHibernate.Type.Int32Type - binding '0' to parameter: 18 2010-08-30 13:20:52,689 [7] [DEBUG] NHibernate.Type.Int32Type - binding '1' to parameter: 19 2010-08-30 13:20:52,704 [7] [DEBUG] NHibernate.Type.StringType - binding 'Mark Gellings' to parameter: 20 2010-08-30 13:20:52,720 [7] [DEBUG] NHibernate.Type.Int32Type - binding null to parameter: 21 2010-08-30 13:20:52,720 [7] [DEBUG] NHibernate.Type.StringType - binding 'null' to parameter: 22 2010-08-30 13:20:52,735 [7] [DEBUG] NHibernate.Type.StringType - binding '130' to parameter: 23 2010-08-30 13:20:52,751 [7] [DEBUG] NHibernate.SQL - begin wc.pkg_crm_load.sp_load_crm_contact(:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18, :p19, :p20, :p21, :p22, :p23 ); end;:p0 = '990000333', :p1 = 'Magazine Group', :p2 = '110 Fifth Avenue', :p3 = NULL, :p4 = 'New York', :p5 = 'NY', :p6 = '10011', :p7 = 'USA', :p8 = 'Mike', :p9 = 'Montemurro', :p10 = NULL, :p11 = '555-5555', :p12 = NULL, :p13 = NULL, :p14 = NULL, :p15 = NULL, :p16 = 1, :p17 = 0, :p18 = 0, :p19 = 1, :p20 = 'Mark Gellings', :p21 = NULL, :p22 = 'null', :p23 = '130' NHibernate: begin wc.pkg_crm_load.sp_load_crm_contact(:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13, :p14, :p15, :p16, :p17, :p18, :p19, :p20, :p21, :p22, :p23 ); end;:p0 = '990000333', :p1 = 'Magazine Group', :p2 = '110 Fifth Avenue', :p3 = NULL, :p4 = 'New York', :p5 = 'NY', :p6 = '10011', :p7 = 'USA', :p8 = 'Mike', :p9 = 'Montemurro', :p10 = NULL, :p11 = '555-5555', :p12 = NULL, :p13 = NULL, :p14 = NULL, :p15 = NULL, :p16 = 1, :p17 = 0, :p18 = 0, :p19 = 1, :p20 = 'Mark Gellings', :p21 = NULL, :p22 = 'null', :p23 = '130'

  1. Support Staff 2 Posted by Paul Batum on 15 Sep, 2010 05:18 PM

    Paul Batum's Avatar

    I've had success with the same approach that you're using. There are a few
    things to be aware of.

    Your problem with the stale state exception should be fixed by using
    no-check. The doco mentions it:
    http://www.nhforge.org/doc/nh/en/index.html#querysql-cud

    I found I had to write wrapper stored procedures that accepted the arguments
    in the exact order that NH was passing them, and then the wrapper would call
    the real stored procedure, reordering as required.

    I'm not familiar with the error stuff you mentioned so I can't offer any
    advice on that front.

    On Wed, Sep 1, 2010 at 11:38 AM, Mark Gellings <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is two plus two?