I'm working on a project that uses nHibernate (v 3.3.2) with fluent where all existing queries are some IQueryOver with something like Linq...
I thought I could avoid learning that confusing syntax by using stored procedures. But calling them has proven to be very difficult.
I tried CreateSqlQuery but got a GenericADOException "could not execute query". I tried defining a .hbm.xml file and loading that in:
m.HbmMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly());
then:
IQuery query = session.GetNamedQuery("sp_GetTagCount");
But the named query was not found. I think that perhaps the XMLfile is not being loaded.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="sp_GetTagCount" callable="true" >
<return class="TagCount" />
exec sp_GetTagCount
</sql-query>
</hibernate-mapping>
That is the only XML or config I have added to the app so perhaps more configuration is required.
I would be happy to use a fluent query instead of the sproc but the only ones I have to copy are single table selects.
The SQL is very simple:
SELECT TOP 10
t.[Id], t.[Name], count(*) as [Count]
FROM [dbo].[Tag] t inner join
[dbo].[TagEntry] te on t.Id = te.TagId
GROUP BY t.[Id], t.[Name]
ORDER BY [Count] DESC
I just need any way at all to get this query into nHibernate. I've tried numerous google searches on "nhibernate fluent tutorial" but all they seem to discuss is DB configuration and class configuration. I need to learn this query syntax. I have spent many hours on this and seem to be making no progress.
If anyone can direct me to a good beginners tutorial to replicate joins, grouping and aggregates in this fluent query syntax that would be great!
I made some progress using Nhibernate.Linq as suggested by Oskar.
I was writing this out:
var results = (from t in session.Query<Tag>()
join e in session.Query<TagEntry>()
But TagEntry doesn't exist in the model.
HasManyToMany(x => x.Entries)
.Table("TagEntry")
.ParentKeyColumn("TagId")
.ChildKeyColumn("EntryId")
.Inverse()
.LazyLoad();
Each Tag has an Entries property which is nice but how do I group by?