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.  [ 2 posts ] 
Author Message
 Post subject: custom sql-insert stored procedure, identity PK
PostPosted: Wed Apr 08, 2009 10:28 am 
Newbie

Joined: Wed Apr 08, 2009 9:42 am
Posts: 1
Hello;

I am using NHibernate 2.0.1.GA and I have following problem:

I've created custom stored procedure (InsertAddress), it's job is simple, just inserting data into table (Address).
In mapping file for class (Address) I used <sql-insert> tag and I call that procedure there: [exec InsertAddress @Street=?, @City=?, @State=?].

After calling:

Address a1 = new Address("Street_val", "City_val", "State_val");
...
session.Save(a1)

I get following error:
NHibernate: exec InsertAddress @Street=@p0, @City=@p1, @State=@p2;
@p0 = 'Street_val', @p1 = 'City_val', @p2 = 'State_val', @p3 = ''

System.Data.SqlClient.SqlException: The parameterized query '(@p0 nvarchar(5),@p1 nvarchar(6),@p2 nvarchar(2),@p3 int)exec In' expects the parameter '@p3', which was not supplied.

NHibernate.Exceptions.GenericADOException: could not insert: [Address][SQL: exec InsertAddress @Street=?, @City=?, @State=?]

Why is NHibernate trying to pass param 'p3' to stored procedure?
-----------------------------

Class (C#):
public class Address
{
public virtual int IDAddress { get; set; }
public virtual string Street { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }

public Address()
{
}

public Address(string Street, string City, string State)
{
this.Street = Street;
this.City = City;
this.State = State;
}
}
-----------------------------

Mapping file:
...
<class name="Address" table="Address">
<id name="IDAddress" column="IDAddress">
<generator class="identity"/>
</id>
<property name="Street" column="Street"/>
<property name="City" column="City"/>
<property name="State" column="State"/>

<sql-insert >exec InsertAddress @Street=?, @City=?, @State=?</sql-insert>
</class>
...
-----------------------------

DB scripts(MSSQL 2005)
Table Address:
create table Address (
IDAddress INT IDENTITY NOT NULL,
Street NVARCHAR(255) null,
City NVARCHAR(255) null,
State NVARCHAR(255) null,
primary key (IDAddress)
)

Stored procedure:
CREATE PROCEDURE [InsertAddress]
(
@Street nvarchar(255),
@City nvarchar(255),
@State nvarchar(255)
)

AS
INSERT INTO Address (Street, City, State)
VALUES (@Street, @City, @State)
RETURN


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 2:07 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
According to the doc sql-insert won't ne uesed with generator identity:

Quote:
Note that the custom sql-insert will not be used if you use identity to generate identifier values for the class.


http://nhforge.org/doc/nh/en/index.html#querysql-cud


Since it's obviuosly used in your case, I suppose hibernate does something wrong. Probably it tries to passe the id as last parameter or something like that.

_________________
--Wolfgang


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