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: Call stored procedure through nhibernate
PostPosted: Wed Sep 01, 2010 3:53 pm 
Newbie

Joined: Tue Aug 31, 2010 9:47 am
Posts: 2
I need to call a stored procedure through nhibernate. But I did not know it make. I have simple stored procedure:

Code:
CREATE PROCEDURE InsertDoc
    @Name nvarchar(50),   
    @Author nvarchar(50),
    @Link nvarchar(50)
AS
  INSERT INTO documents(name, date, author, doclink)
  VALUES(@Name, CURRENT_TIMESTAMP, @Author, @Link)

I do this in my code:

Code:
public class documents
{
public int id;
public string name;
public DateTime date;
public string author;
public string doclink;

public void CreateDocuments(String n,String l,String u)
{
documents exSample = new documents();
exSample.name = n;
exSample.date=DateTime.Now;
exSample.author=u;
exSample.doclink=l;

using (ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
//Session.CreateSQLQuery("EXEC :sp_name :start_date :end_date").SetString("sp_name", <>;)
session.CreateSQLQuery("EXEC InsertDoc @Name = N'" + exSample.name + "',@Author = N'" + exSample.author + "',@Link = N'" + exSample.doclink + "'");
// session.Save(exSample);
transaction.Commit();
}
}
}

public ISessionFactory factory;

public ISession OpenSession()
{
if (factory == null)
{
Configuration conf = new Configuration();
conf.AddAssembly(Assembly.GetCallingAssembly());
factory = conf.BuildSessionFactory();
}
return factory.OpenSession();
}
}
}

I call the stored procedure

Code:
session.CreateSQLQuery("EXEC InsertDoc @Name = N'" + exSample.name + "',@Author = N'" + exSample.author + "',@Link = N'" + exSample.doclink + "'");

In my mapping file I have these settings:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="WebApplication1" assembly="WebApplication1">
  <class name="WebApplication1.documents" table="documents" lazy="false">
    <id name="id" access="field">
      <generator class="native" />
    </id>
    <property name="name" access="field" column="name" type="String"/>
    <property name="date" access="field" column="date" type="date"/>
    <property name="author" access="field" column="author" type="String"/>
    <property name="doclink" access="field" column="doclink" type="String"/>
  </class>
</hibernate-mapping>


help me to solve this problem or give useful link

CreateSQLQuery was executed without error, but entry is not added to the table


Top
 Profile  
 
 Post subject: Re: Call stored procedure through nhibernate
PostPosted: Fri Sep 03, 2010 2:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I don't think insert via sp is working in with CreateSQLQuery. Have a look here:

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

_________________
--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.