I spent some time reworking my environment so I could run this locally under my own control instead of under some else's environment remotely.
Good news is, the query works now, sort of. The problem is I got fewer results than I think I should be getting.
First, let's make this example as much like my real scenario as possible. Let's amend the definition of the BLOG_ENTRY table like this:
Code:
|------BLOG_ID----|----POST_DATE------|-----POS_STATUS-----|-----POST_TYPE-----|
17 2005-11-15 1 3
17 2005-11-29 1 3
18 2005-11-22 1 3
18 2005-11-30 1 3
19 2005-10-02 1 3
The following code yields 22 rows.
Code:
Query q = session.createQuery("select a from BlogEntry a where" + "a.postDate = (select max(b.postDate) " +
"from BlogEntry b " +
"where a.blogId = b.blogId " +
"and a.postStatus = 1 " +
"and a.postType = :postType)");
q.setLong("postType", postType); // postType is 3
result = q.list();
I also tried this HQL which yields 22 rows:
Code:
select a from BlogEntry a where a.postDate = (select max(b.postDate)
from BlogEntry b
where a.blogId = b.blogId) and a.postStatus = 1 and a.postType = :postType
The following SQL code yields 61 rows when I run it directly in MySQL Query Browser against the same DB.
Code:
SELECT * FROM
BLOG_ENTRY ENTRIES
,(SELECT BLOG_ID, MAX(POST_DATE)
FROM BLOG_ENTRY
GROUP BY BLOG_ID) BLOGS
WHERE
ENTRIES.BLOG_ID = BLOGS.BLOG_ID
AND ENTRIES.POST_STATUS = 1
AND ENTRIES.POST_TYPE = 3
So, I'm pleased that now have valid syntax, but I still need to figure out why I'm not getting 61 rows like the SQL does.
Any ideas?