HQl查询sql查询?

时间:2014-02-20 05:51:18

标签: sql mapping hql

以下是一个SQL查询,我希望hql查询相同??任何建议将不胜感激。

在这个查询中我试图获取两个表'blogs'和'askquestions'的记录,这些记录没有相互映射。

  SELECT blog_title as title ,created_date as date FROM  blog  UNION SELECT ask_question as title , created_on as date FROM askquestions ) as aa ORDER BY date DESC

2 个答案:

答案 0 :(得分:0)

遗憾的是,HQL目前尚不支持UNIONUNION ALL

您仍然可以尝试以下方法:

// Create two query strings for two sub HQLs
String firstHQLQueryStr = " from blog b where <criteria1>";
String secondHQLQueryStr = "from askquestions a where <criteria2>";

// Divide the two select clauses into two sub HQLs
Query query1 = session.createQuery(firstHQLQueryStr);
Query query2 = session.createQuery(secondHQLQueryStr);

// Fetch the data from database using these two sub HQLs first
List firstDataList= query1.list();
List secondDataList= query2.list();

//Combine the results
firstDataList.add(secondDataList);

// Collection equivalent to UNION ALL
List nonUniqueCollection =  firstDataList;

//Collection equivalent to Union (as non-unique elements are removed)
HashSet uniqueCollection = new HashSet(nonUniqueCollection );

希望有所帮助。

答案 1 :(得分:0)

试试这个......

select * from ((blog_title as title ,created_date as date FROM  blog) union (SELECT ask_question as title , created_on as date FROM askquestions)) as a order by a.date desc;