简体   繁体   English

select子句中的休眠条件SubSelect

[英]Hibernate Criteria SubSelect in select clause

Hello guy's having a bit of a problem writing a subselect query with criteria API sublselect should take a parameter from selected q 你好,使用标准API sublselect编写子选择查询时遇到了一些问题,应该从选择的q中获取一个参数

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 = ?

As you can see the totalout suselected against c.file_id I was playing with this for 2 days with no luck maby sombody in stackoverflow community who has a strong java and hibernate knowledge would be able to help me.. This is what I got getting exeption 2012-01-17 01:48:06,808 ERROR [org.hibernate.util.JDBCExceptionReporter] - (session F2C61D08758E0AA09CA6C99A5B6F2145, thread 113 invoke ByDefenceFirm.getByDefenceFirm) 如您所见,对c.file_id的总选择是我选择的,而我在这里进行了2天,而在stackoverflow社区中没有运气的maby sombody,他拥有强大的Java和冬眠知识将能够为我提供帮助。.这就是我得到启发的原因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(); 

If anybody have any suggestions would really appreciated it. 如果有人有任何建议,将不胜感激。

A subselect in the select clause is impossible with Criteria. 在条件子句中,select子句中的子选择是不可能的。 You'll have to go with a HQL or a SQL query. 您必须使用HQL或SQL查询。

It is possible. 有可能的。 At least now with the current version of Hibernate. 至少现在使用最新版本的Hibernate。 Use "Restrictions.sqlRestriction()". 使用“ Restrictions.sqlRestriction()”。

For example: 例如:

Criteria accountCriteria = getCurrentSession().createCriteria(Account.class);
accountCriteria.add(Restrictions.sqlRestriction("{alias}.field IN (SELECT ......)"));
@SuppressWarnings("unchecked")
List<Account> accounts = accountCriteria.list();

Sorry, I didn't read the question carefully. 抱歉,我没有仔细阅读问题。 Your scenario is different. 您的情况不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM