We're using NHibernate.Mapping.Attributes to do our mappings.
In order to get NHibernate to populate a data object automatically, it appears that you need to allow for an identity column. Okay, no problem, I added a (fake) PK column to my stored procedure results (it's a report query, so the identifier doesn't need to mean anything as long as it's unique).
I've got the code in a state where it's returning the results of the stored procedure in the data objects, but the Id
column is always being populated with -1 instead of the actual PK identity from the query (which I verified is coming out correctly). I was looking at this example and this example, but I can't figure out how to do the Id
column mapping to get it to populate the column properly. (I honestly don't care about the column itself and I can proceed without fixing this, but the Id
property has to be public, so it should return proper values -- any anyone reading this back later who has the same problem may actually need the proper values.)
Here's what I have so far (massively simplified):
Stored procedure results:
Id FileNumber 1 10-01 2 10-02 3 10-02 4 10-03
MyReportQuery.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="MyReportQuery">
<query-param name="param1" type="System.Boolean" />
<return class="MyCompany.Core.Domain.Reports.MyReportDataObject, Core" />
<![CDATA[
exec MyReportQuery :param1
]]>
</sql-query>
</hibernate-mapping>
MyReportQuery.cs:
results = session.GetNamedQuery("MyReportQuery")
.SetParameter<bool>("param1", true)
.List<MyReportDataObject>();
MyReportDataObject.cs:
namespace MyCompany.Core.Domain.Reports
{
[Class]
public class MyReportDataObject
{
private int _id = -1;
private string _fileNumber = null;
// This is where I need help
[Id(0, Column = "Id", TypeType = typeof(int))]
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
[Property(Column = "FileNumber")]
public virtual string FileNumber
{
get { return _fileNumber; }
set { _fileNumber = value; }
}
}
}
As I said before, this code does populate the data objects properly, all except for the Id
column. I've tried messing about with the attributes, but nothing has worked so far.
Any help would be appreciated. A solution using NHibernate.Mapping.Attributes directly would be nice, but if you can solve this with an XML-style mapping, I should be able to translate it over. Thanks!