Nhibernate Stored Procedure Mapping in xml file clarification - nhibernate

Can anyone tell where to map Stored-Procedures exactly in Nhibernate.
In class hbm.xml file or newly declared file(hbm.xml) especially for Stored Procedures???
can u tell with reason??

I create a single XML file that contains ALL stored procedure calls, e.g.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="SummaryReport">
exec getSummaryReport :productId
</sql-query>
<sql-query name="FullReport">
exec getFullReport :productId
</sql-query>
</hibernate-mapping>
and mark this as an embedded resource. I can then call me SP like this:-
var results = Session
.GetNamedQuery("SummaryReport")
.SetInt32("productId", productId);
.SetResultTransformer(
new AliasToBeanResultTransformer(typeof(SummaryReport)));
return results.List<SummaryReport>();
This works fine for me but there really isn't a recommended way, its always down to what you feel is right for you.

Related

Nhibernate Named SQL Query

Can any body tell me what is wrong with below query Named SQL Query using NHibernate.
As I am getting error - "Error in Named Query : GetSecondReviewIncomeStatements ":
<sql-query name="GetSecondReviewIncomeStatements" read-only="true">
<![CDATA[
SELECT I.TotalNetSales,I.CostOfGoodsSold,I.GrossProfit
FROM IncomeStatement as incomeStatement
INNER JOIN FETCH CompSearchResultItem as resultItem
ON incomeStatement.Comparable.ID = resultItem.Comparable.ID
AND resultItem.CompSearch.ID = :compSearchID
]]>-->
</sql-query>-->
I have searched a lot on the web, some say instead on On put Where.
I also tried NamedQuery only but everytime I get the same error.
Please check the Format for Named Query in .hbm.xml file.
in hbm.xml
<sql-query name="ShowProducts">
<return alias="Product" class="Product" />
exec ShowProducts
</sql-query>
<sql-query name="DeleteProducts">
<query-param name="Id" type="int"></query-param>
exec DeleteProducts #ID=:Id
</sql-query>
in Code Usage
IQuery query = (IQuery)session.GetNamedQuery("ShowProducts");
var listProducts = query.List<Product>();
GridView1.DataSource = listProducts;
GridView1.DataBind();
Does your SQL runs if you put it 'as-is' in your rdbms sql executor?
Besides the strange --> after the enclosing CDATA element, I've noticed that you haven't declared the I alias in your query.
I mean, these columns preceded with I...
SELECT I.TotalNetSales,I.CostOfGoodsSold,I.GrossProfit
...are coming from where?

NHibernate mapping by code equivalent for <database-object>

I have an NHibernate mapping that makes use of a <database-object> element that makes use of a definition class. The mapping looks a bit like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Foo" assembly="Foo">
<database-object>
<definition class="Foo.AuxilliaryData,Foo"/>
</database-object>
</hibernate-mapping>
This works fine, but I would like to use mapping by code as per NHibernate >=3.2. Is there a way to do this?
(Out of interest, my reason for wanting to convert this is so that R# "Find usages" will work for the AuxilliaryData class. At the moment R# (and any other code inspection tool) thinks that this class is unused).
just add it when generating the Configuration object
config.AddAuxiliaryDatabaseObject(new Foo.AuxilliaryData());

Named query not known

I have a mapping in NHibernate like this:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="News.BusinessEntity" assembly="News.BusinessEntity" xmlns="urn:nhibernate-mapping-2.2">
<class name="News" table="News">
<property name="NewsId" type="Int64" column="NewsId" />
<property name="NewsTitle" type="String" column="NewsTitle" />
</class>
<sql-query name="Sp_News">
<return class="News" />
exec Sp_News
</sql-query>
</hibernate-mapping>
When I call session.GetNamedQuery("Sp_News") I get the following error:
Named query not known: Sp_News.
Any help would be much appreciated.
the error was solved just by setting the Build Action property of .hbm.xml file to Embeded Resource
The same error is thrown if the mapping file doesn't end with .hbm.xml.
I have accidentally renamed a mapping file and dropped the .hbm. from the name, putting it back solved this issue for me.

property formula not returning the result

I have a table Submission, which has a many to one mapping to a SubmissionType, which has a one to many mapping to SubmissionTypeYear table. Even though this is a one-to-many mapping, there will always be just one SubmissionTypeYear for a SubmissionType. (Don't ask me why the database was designed like this. I wasn't involved and I just have to make it work.)
Therefore, a submission will have a SubmissionType will have a SubmissionTypeYear.
My problem is that in my Submission.hbm.xml, I want a calculated property, which gets me a column value from SubmissionTypeYear. But I don't know what's wrong, it comes back with a zero, when I know that there is a value in the table.
This is my code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Business.Domain" assembly="Business.Domain">
<class name="Business.Domain.Submission, Business.Domain" table="dbo.Submission" lazy="true" optimistic-lock="version" >
<id name="Id" column="SubmissionId">
<generator class="identity"/>
</id>
<discriminator column ="SubmissionTypeCode"></discriminator>
<many-to-one name="SubmissionTypeMember" insert="false" column="SubmissionTypeCode" class="RefSubmissionType" access ="field.camelcase-underscore" cascade ="none" />
<property name="Year" formula="(SELECT TOP 1 SubmissionTypeYear.Year FROM SubmissionTypeYear WHERE SubmissionTypeYear.SubmissionTypeCode = SubmissionTypeCode)" access="field.camelcase-underscore"></property>
</hibernate-mapping>
I thought that it wasn't picking up the SubmissionTypeCode correctly, so I tried with a static string as well, but doesn't seem to be working.
What am I doing wrong? Please help.
Thanks
OK, I figured it out. The problem was with the new Submission types created. As it was a new object and not yet stored in the database, it wasn't able to pick up the calculated field. It was working for all the existing objects. Fixed :)
Hope this helps someone else.

How to transform query?

i have an sql query that has one unnamed column as a list of strings.
my hbm is declared as follows:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Services.Data" namespace="Services.Data" >
<sql-query name="GetDiagramSubscriptions">
exec Diagram_Subscriptions:contactId
</sql-query>
</hibernate-mapping>
my repository method:
IQuery query = Session.GetNamedQuery("GetDiagramSubscriptions")
.SetInt32("contactId", contactId)
.SetResultTransformer(Transformers.AliasToBean<string>());
return query.List<string>();
this doesn't work because type string doesn't have a setter.
i don't want to declare a mapping class just for this one column. is there a way to transform this to a Tuple or something?
just remove this call .SetResultTransformer(Transformers.AliasToBean<string>()) and the List<string>() will do what you expect.