First of all, sorry for the rather confusing title, but my problem consists of 2 parts.
Hibernate version: 1.2.0
What I have, is this:
I have a class 'Project' and a class 'Report'. A report belongs to a Project, however, I've chosen not to have a collection of Reports in Project.
A Project does have a collection of 'ProjectMembers' and a ProjectMember references a user.
To make this maybe more clear, this is my data-model:
My Project class looks like this:
My Report class looks like this:
This is the mapping file for the Project class:
This is the mapping file for the Report class:
Now, what I want to do, is this: given a Project object, I want to retrieve all the Reports that belong to that Project.
Simple enough I thought: I create a method 'GetReportsForProject( Project p )' in my ReportsRepository which would execute a HQL query that looks like this:
Code:
return theSession.
CreateQuery ("from Report where BelongsToProject.Id = :pid").SetGuid ("pid", p.Id).List<Report> ();
Unfortunately, this fails at execution time. This HQL gets translated to the following SQL:
Code:
select report0_.ReportId as ReportId1_, report0_.ProjectId as ProjectId1_, report0_.StatusId as StatusId1_, report0_.ErrorMessage as ErrorMes4_1_, report0_.StackTrace as StackTrace1_, report0_.VersionNumber as VersionN6_1_, report0_.CreationDate as Creation7_1_
from bugradar.dbo.Reports report0_
where (BelongsToProject.Id=? )
As you can see, the HQL doesn't get translated to
'WHERE report.projectid = ?'
How come ? Am I something missing in my mapping file ?
Then, I tried to achieve what I wanted using a Criteria Expression, like this:
Code:
ICriteria c = SessionManager.Instance.Session.CreateCriteria (typeof (Report));
c.Add (Expression.Eq ("BelongsToProject", p));
return c.List<Report> ();
Unfortunately, this fails as well with an exception (NHibernate.ADOException), in which the InnerException says:
Quote:
identifier type mismatch Parameter name: id
I don't understand what this exaclty means; the stacktrace is this:
Code:
" at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)\r\n at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)\r\n at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results)\r\n at NHibernate.Impl.SessionImpl.Find[T](CriteriaImpl criteria)\r\n at NHibernate.Impl.CriteriaImpl.List[T]()\r\n at SoftwareFg.BugRadar.Domain.Repositories.NHibernate.ReportRepository.GetReportsForProject(Project p)
However, when I look in SQL Profiler, I see that the SQL query gets constructed correctly, and when I paste the query that I see in SQL profiler in Query Analyzer, I can execute it correctly.
Any ideas what the problem could be ?
Concerning the Criteria problem, I've found
this JIRA case. Could it be related ?