2

Hello guy's having a bit of a problem writing a subselect query with criteria API sublselect should take a parameter from selected 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)

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.

Alexander
  • 17
  • 1
  • 1
  • 3

2 Answers2

5

A subselect in the select clause is impossible with Criteria. You'll have to go with a HQL or a SQL query.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I do it in HQL constantly ... SQL is not an option. I have Found this http://stackoverflow.com/questions/4834372/hibernate-criteria-on-collection-values But was unable to implement it. Thanks for your responce – Alexander Jan 17 '12 at 08:53
  • Sorry, you're right. Subqueries can't be in the from clause, but they can be in the select clause. I stand to my point regarding criteria, though: no subqueries in a select clause using criteria. – JB Nizet Jan 17 '12 at 09:01
0

It is possible. At least now with the current version of Hibernate. Use "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.

oᴉɹǝɥɔ
  • 1,796
  • 1
  • 18
  • 31