-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Deepcopy of Criteria
PostPosted: Mon Jul 14, 2008 8:10 am 
Newbie

Joined: Mon Jul 14, 2008 7:28 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.