stored procedures - nhibernate sql-query results different from direct query results -
Update: I have answered my question below, take a peek and tell me if you have The best way is to get a list with <1, 2} result when calling my sqlserver function from SQL Management Studio
Executive Summary: , 3}. I get this list {1, 1, 1} when calling method using code using NHibernate. ('1' is a complete result line, scalar is not '1') I have also experimented with different data sets and found the same behavior.
Long story:
I have a SQL function in SQL Server, which is called GetHistory (itemId). It gives a table with results when I ask SQL Management Studio I get a list of results. I query the function like this:
GetHistory (10001) to < Select> *
On the given DB, this results in 3 rows. Each row has a time, type and description.
In NHibernate, I have specifically created a new unit for this, because there is no such organic table / unit so I have the mapping:
& Lt; Generator class = "parent" /> & Lt; / Id & gt; & Lt; Property name = "type" column = "type" type = "small" access = "field" /> & Lt; Property name = "time" column = "time" type = "date time" access = "field" /> & Lt; Property Name = "Description" column = "Description" type = "string" access = "field" /> & Lt; / Square & gt; & Lt; Sql- queryname = 'GetHistory' & gt; & Lt; Return class = 'HistoryEvent, myAssembly' aliases = 'history events' / & gt; & Lt ;! [CDATA [SELECT * FROM GetHistory (: ID)]] & gt; & Lt; / SQL query & gt; & Lt; / Hibernate-mapping & gt;
The business object looks like this:
PublicEventEventEvent {Personal Long ID; Private Small Type; Private string description; Private Datetime Time; ... there are properties with public allowance etc ...}
And finally, I call this function from my code like this:
IList results = S.GetNamedQuery ("GetHistory"). SetInt64 ("id", id). List ();
While inspecting this list with the debugger, I get 3 entities that have the same row 3 times. Instead of going through the DB function, I query the query directly from NHibernate (SQL- Tried using the query), but found the same result.
My intuition is that there is something wrong with my mapping or something is wrong with NHibernate :)
HELP!
I have really helped you by writing everything I = this was the problem:
The ID I wrote Had used it was not unique. This is an ID, not just the right ID for the query. Why is it like this? Because my query gives a consort of three questions from three different tables, and the id returned was actually the item id (the same function which was passed!)
So now I A unique ID I did try briefly to use the composite ID tag of NHibernate instead of ID - but facing difficulty I did not spend much time with myself and decided to return the ID from the query.
Find out I can not use a selection variable (in) ... inside a task with a table variable, I considered RAND for one second and finally ROW_NUMBER () my fake ID It works quite well, though this code will not win a beauty contest.
PS: Better suggestions have been given to generate an ID? Or have you ever got a composite ID to work for you? Let me know ...
Comments
Post a Comment