-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL doesn't get translated to SQL and problem with Criteria
PostPosted: Mon Jul 21, 2008 1:50 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
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:

Image

My Project class looks like this:

Image

My Report class looks like this:

Image

This is the mapping file for the Project class:

Image

This is the mapping file for the Report class:

Image

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 ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 4:23 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 3:07 pm
Posts: 77
A collegue of mine came with a golden hint. He told me, what if you use aliasses in your HQL.

In a previous project where I've used NHibernate, I used aliasses and i never experienced this problem. Now, i didn't use aliasses, so I did, and indeed, now my query works... Why ?

Also, the problem with the Criterium still keeps me flabbergasted.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.