title: Hibernate Criteria queries and how to efficiently join data?
body: <p>"THIS IS MY SQL THAT I WANT TO CONVERT TO CRTIERIA:</p>
  
  <p><i>select be.* from BlogEntry be join Blog b on be.blog=b.id join Follower f on b.id=f.blogId where be.publishStatus='published' and be.secured=false and f.user=? union select be1.* from BlogEntry be1 join SecureUser s on be1.id=s.blogEntryId join User u on s.userProfile=u.userProfile and u.id=? order by publishDate desc";</i></p>
  
  <p>hello, folks. i have been trying to use HQL and native SQL to execute the above query and i have been frustrated at every turn, for the most part because doing UNION is super awkward in Hibernate.  even if you try a SQLQuery you still have the whole mess of establishing your entity relationships by being FORCED to include every single attribute of every subclass referenced in the SQL.  this is proving to be a total pain to get past.  </p>
  
  <p>SO, i am moving on to a possible criteria query solution, but i think i need some help.  the query below is totally fine in MySQL workbench and fast as lightning.  the hump i am trying to get over with the criteria query is that some of my entity relationships are defined by foreign key references in the tables and some are not.  when they ARE, i can, of course, do something like this (which evaluates part of my query, before the UNION):</p>
  
  <p><i>ExtendedDetachedCriteria entryDetachedCriteria = extendedDetachedCriteria.forClass(BlogEntry.class);
  entryDetachedCriteria.createAlias("blogEntry","blogEntry");
  entryDetachedCriteria.createAlias("blogEntry.blog", "blog");</i></p>
  
  <p>etc, etc...</p>
  
  <p>HOWEVER, when i am joining data in a different way, like in this portion of the SQL:</p>
  
  <p><i>select be1.* from BlogEntry be1 join SecureUser s on be1.id=s.blogEntryId (no actual foreign key relationship defined in the tables, SecureUser entities are just stamped with the relevant BlogEntry ID when they are created)</i></p>
  
  <p>how should i write the criteria queries differently from the way demonstrated above?  </p>
  
  <p>i realize that questions like these are a total pain to get your head around if you are not already knee deep in trying to solve - please excuse the convoluted-ness of the question i am asking.  i would deeply appreciate any guidance someone could offer, even if it's "get your hibernate act together, ya doofus!".  sort of stuck at the moment.</p>