Hi,
I am working with HibernateCritera API on a quite simple problem: "Find the sum of all positive values and find the sum of all negative values in a table" ... i get the Criteria from the framework to determine the intersection of the table.
the simple approach:
Code:
Criteria c1 = criteria.getExecutableCriteria(s);
Criteria c2 = criteria.getExecutableCriteria(s);
umsatz = (BigDecimal) c1.setProjection(Projections.sum("betrag")).add(Restrictions.gt("betrag", BigDecimal.ZERO)).uniqueResult();
kosten = (BigDecimal) c2.setProjection(Projections.sum("betrag")).add(Restrictions.lt("betrag", BigDecimal.ZERO)).uniqueResult();
doesnt work since criteria.getExecutableCriteria() returns the same value twice - therefore c1=c2 - so the Restrictions add and "kosten=null" - thats wrong
The workaround:
Code:
try {
c = (DetachedCriteria) SerializableUtils.deepCopy(criteria);
umsatz = (BigDecimal) c1.setProjection(Projections.sum("betrag")).add(Restrictions.gt("betrag", BigDecimal.ZERO)).uniqueResult();
Criteria c2 = c.getExecutableCriteria(s);
kosten = (BigDecimal)c2.setProjection(Projections.sum("betrag")).add(Restrictions.lt("betrag", BigDecimal.ZERO)).uniqueResult();
} catch (IOException ex) {
Logger.getLogger(KostentraegerAuswertung.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(KostentraegerAuswertung.class.getName()).log(Level.SEVERE, null, ex);
}
does work perfect, but is really bad code.
RFC
ThLandgraf