I have a class called Book that is mapped to a table called [Books]. I have a separate table called [Pages] that is mapped to a Page class. I need the Book class to have a List<Page> property that is populated using a custom native SQL query. I need the Page class to be populated from the table and the SQL query is not a simple one-to-many relationship. A native SQL query is mandatory to populate this.
As stated previously, this is not possible in nHibernate, because you can't populate a class using a custom SQL statement AND a have a table reference. I would either need to create 2 "Page" classes one to populate from the table and another to populate with the query. These is very much against best practices.
Take a look at these nHibernate links I have found:
In this one, the object that is being populated by the stored procedure doesn't reference a table:
-
http://ayende.com/Blog/2006/09/18/Using ... dures.aspx
He doesn't give a class table in this topic:
-
http://forum.hibernate.org/viewtopic.php?p=2376446
"what if you don't have a table like "Employment" to return, and you do want to return a dataset that doesn't map to any table?
I played with nHibernate a litte bit and find a way to circumvent this limitation. What you need to do is create an empty table with columns maps to the dataset you want to return. Create its hbm mapping and C# domain model class too. You don't have insert any data into this table. Then nHibernate will be able to call the stored procedure and return the dataset you want. The downside of this approach is you don't want to create and maintain a lot of empty tables to just make nHibernate happy."
-
http://www.dalun.com/blogs/08.29.2007.htm
And if I managed to get all of this to work, I couldn't use a stored procedure.
"Notice that stored procedures currently only return scalars and entities. <return-join> and <load-collection> are not supported."
-
http://nhforge.org/doc/nh/en/index.html
I have played with my code for at least 15 hours trying to get this to work and nothing I have found contracts this assumption. You can only populate a class either from 1 sql query OR a sql table. You can't use both.
nHibernate needs to seriously fix its documentation. The documentation page was of little or no help, because it fails to provide enough code and explanation to make it a viable resource. The only way a person can "learn" how to use nHibernate is by jumping from blog to blog, piecing bit by bit together until something is slapped together.