你好傢伙有一點寫一個子查詢的查詢與標準API sublselect的問題應採取的參數從選定q休眠條件的子查詢在SELECT子句
SELECT c.file_id as fid, s.person as person, s.fax_no as fax, s.phone_no as phone,(SELECT SUM(b.ammount)
FROM billing b
WHERE b.constants_file_id = c.file_id
) - (
SELECT
CASE
WHEN b.partioa_pay IS NULL
THEN "0"
ELSE SUM(b.partioa_pay)
END
FROM billing b
WHERE b.constants_file_id = c.file_id) AS totalout
FROM constants c
LEFT JOIN firm_management s ON c.firm_management_firm_managment_id = s.firm_managment_id
WHERE s.firm_managment_id = ?
正如你可以看到suselected針對c的totalout。 file_id 我在玩這個2天沒有運氣maby sombody在stackoverflow社區誰擁有強大的Java和冬眠知識將能夠幫助我.. 這就是我得到的exeption 2012-01-17 01:48 :06,808錯誤[org.hibernate.util.JDBCExceptionReporter] - (會話F2C61D08758E0AA09CA6C99A5B6F2145,線程113調用ByDefenceFirm.getByDefenceFirm)
Criteria crit = session.createCriteria(Constants.class, "co");
crit.createAlias("co.provider", "pr", Criteria.INNER_JOIN)
.createAlias("co.firmManagement", "ins", Criteria.LEFT_JOIN)
.createAlias("co.law", "lw", Criteria.LEFT_JOIN)
.createAlias("co.billing", "bl", Criteria.LEFT_JOIN)
.setProjection(Projections.projectionList()
.add(Projections.property("co.fileId"))
.add(Projections.property("co.status"))
.add(Projections.property("co.asi"))
.add(Projections.property("pr.name"))
.add(Projections.property("ins.companyName"))
.add(Projections.property("lw.shortName")));
DetachedCriteria valueCrit = DetachedCriteria.forClass(Billing.class, "bl")
.setProjection(Projections.sum("bl.ammount"))
.setProjection(Projections.property("bl.constants"))
.add(Restrictions.eqProperty("bl.constants", "co.fileId"));
crit.add(Property.forName("co.fileId").eq(valueCrit));
crit.setProjection(Projections.property("co.billing"));
crit.add(Restrictions.eq("lw.lawOfficeId", prid));
resultList = crit.list();
如果有人有任何建議,會真的很感激它。
我在HQL中不斷做它... SQL不是一個選項。我發現這個http://stackoverflow.com/questions/4834372/hibernate-criteria-on-collection-values但無法實現它。感謝您的回覆 – Alexander 2012-01-17 08:53:29
對不起,你是對的。子查詢不能位於from子句中,但它們可以位於select子句中。儘管如此,我仍然堅持我的觀點:在使用標準的select子句中沒有子查詢。 – 2012-01-17 09:01:45