-->
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.  [ 2 posts ] 
Author Message
 Post subject: sum() on collection giving bad results
PostPosted: Tue Oct 31, 2006 5:08 pm 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2cr4
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.wazollc.alphatheory.hibernate.bo">
<class name="FundAssetBO"
table="`FUNDASSET`"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
lazy="true"
batch-size="6"
>
<id name="id" type="java.lang.Long" column="`fundAssetID`">
<generator class="native" />
</id>

<property name="created" column="`created`"
type="timestamp"
not-null="false" length="6" />
<property name="modified" column="`modified`"
type="timestamp"
not-null="false" length="6" />
<property name="createdUserID" column="`createdUserID`"
type="java.lang.Long" not-null="false" />
<property name="modifiedUserID" column="`modifiedUserID`"
type="java.lang.Long" not-null="false" />
<property name="currentShares" column="`currentShares`"
type="java.lang.Double" not-null="false" />
<property name="washRuleDate" column="`washRuleDate`"
type="timestamp" not-null="false" length="6" />
<property name="maxPositionSize" column="`maxPositionSize`"
type="java.lang.Long" not-null="false" />
<property name="maxPositionReason" column="`maxPositionReason`"
type="java.lang.String" not-null="false" />

<many-to-one
name="asset"
class="com.wazollc.alphatheory.hibernate.bo.AssetBO"
column="`assetID`"
cascade="all"
not-null="true"
/>

<many-to-one
name="fund"
class="com.wazollc.alphatheory.hibernate.bo.FundBO"
column="`fundID`"
cascade="save-update"
not-null="true"
/>

<many-to-one
name="status"
class="com.wazollc.alphatheory.hibernate.bo.StatusBO"
column="`statusID`"
cascade="save-update"
not-null="true"
/>

<many-to-one
name="category"
class="com.wazollc.alphatheory.hibernate.bo.CategoryBO"
column="`categoryID`"
cascade="save-update"
not-null="false"
/>

<many-to-one
name="annualizeReturnOptions"
class="com.wazollc.alphatheory.hibernate.bo.AnnualizeReturnOptionsBO"
column="`annualizeReturnOptionsID`"
cascade="save-update"
not-null="true"
/>

<many-to-one
name="expectedReturnCalculationOptions"
class="com.wazollc.alphatheory.hibernate.bo.ExpectedReturnCalculationOptionsBO"
column="`expectedReturnCalculationOptionsID`"
cascade="save-update"
not-null="true"
/>

<many-to-one
name="analysisConfidenceOptions"
class="com.wazollc.alphatheory.hibernate.bo.AnalysisConfidenceOptionsBO"
column="`analysisConfidenceOptionsID`"
cascade="save-update"
not-null="true"
/>

<set name="fundAssetTradeHistory" table="`fundassettradehistory`" inverse="true" order-by="shares desc" cascade="all" lazy="true">
<key column="`fundassetID`" />
<one-to-many class="com.wazollc.alphatheory.hibernate.bo.FundAssetTradeHistoryBO" />
</set>


</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.wazollc.alphatheory.hibernate.bo">
<class name="FundAssetTradeHistoryBO"
table="`FUNDASSETTRADEHISTORY`"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
lazy="true"
batch-size="6"
>
<id name="id" type="java.lang.Long" column="`tradeHistoryID`">
<generator class="native" />
</id>

<property name="created" column="`created`"
type="timestamp"
not-null="false" length="6" />
<property name="modified" column="`modified`"
type="timestamp"
not-null="false" length="6" />
<property name="createdUserID" column="`createdUserID`"
type="java.lang.Long" not-null="false" />
<property name="modifiedUserID" column="`modifiedUserID`"
type="java.lang.Long" not-null="false" />
<property name="occurenceDate" column="`occurenceDate`"
type="timestamp"
not-null="false" />
<property name="shares" column="`shares`"
type="java.lang.Double"
not-null="false" />
<property name="price" column="`price`"
type="java.lang.Double"
not-null="false" />

<many-to-one
name="fundasset"
class="com.wazollc.alphatheory.hibernate.bo.FundAssetBO"
column="`fundassetID`"
cascade="save-update"
not-null="true"
/>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():


Full stack trace of any exception that occurs:

Name and version of the database you are using:
mssql 9.0

The generated SQL (show_sql=true):
select fundassetb0_.[fundAssetID] as col_0_0_, sum(fundassett4_.[shares]) as col_1_0_ from [FUNDASSET] fundassetb0_ left outer join [STATUS] statusbo1_ on fundassetb0_.[statusID]=statusbo1_.[statusID] left outer join [ASSET] assetbo2_ on fundassetb0_.[assetID]=assetbo2_.[assetID] left outer join [ATUser] atuserbo5_ on assetbo2_.[analystID]=atuserbo5_.[atUserID] left outer join [DEPARTMENT] department6_ on atuserbo5_.[departmentID]=department6_.[departmentID] left outer join [FUND] fundbo3_ on fundassetb0_.[fundID]=fundbo3_.[fundID] left outer join [FUNDASSETTRADEHISTORY] fundassett4_ on fundassetb0_.[fundAssetID]=fundassett4_.[fundassetID] where 1=1 and atuserbo5_.[atUserID]=? and department6_.[departmentID]=? and fundbo3_.[fundID]=? group by fundassetb0_.[fundAssetID] , fundassett4_.[shares] , statusbo1_.[statusID] order by statusbo1_.[statusID] asc

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

Hi all, I'm running this code which is giving me strange results. I'm sure I'm doing something wrong. Here's my code:

Code:
public List<FundAssetHelper> findFundAssetListAndSharesSum(FundAssetDTO faDTO) {

        List<FundAssetHelper> result = new ArrayList<FundAssetHelper>();
        String strQuery = "select new "
                + "com.wazollc.alphatheory.hibernate.dao.FundAssetHelper("
                +    "fa, sum(fath.shares)) from FundAssetBO fa "
                + "LEFT JOIN fa.status status "
                + "LEFT JOIN fa.asset asset "
                + "LEFT JOIN fa.fund fund "
                + "LEFT JOIN fa.fundAssetTradeHistory fath "
                + "LEFT JOIN asset.analyst ATUserBO "
                + "LEFT JOIN ATUserBO.department department "
                + "WHERE 1 = 1 ";
                if (faDTO.getAnalystID() != null) {
                    strQuery = strQuery + "AND " + "ATUserBO.id = :analystID ";
                }
                if (faDTO.getDepartmentID() != null) {
                    strQuery = strQuery + "AND " + "department.id = :departmentID ";
                }
                if (faDTO.getFundID() != null) {
                    strQuery = strQuery + "AND " + "fund.id = :fundID ";
                }
                if (faDTO.getStatusID() != null) {
                    strQuery = strQuery + "AND " + "status.id = :statusID ";
                }
                strQuery = strQuery + "group by fa.id, fath.shares,status.id ";
                strQuery = strQuery + "Order by status.id asc";

        Query query;
        query = getSession().createQuery(strQuery);
        if (faDTO.getAnalystID() != null) {
            query.setParameter("analystID", faDTO.getAnalystID()); 
        }

        if (faDTO.getDepartmentID() != null) {
            query.setParameter("departmentID", faDTO.getDepartmentID());       
        }
       
        if (faDTO.getFundID() != null) {
            query.setParameter("fundID", faDTO.getFundID());
        }

        if (faDTO.getStatusID() != null) {
            query.setParameter("statusID", faDTO.getStatusID());
        }
       
        result = query.list();
        if (logger.isDebugEnabled()) {
            for (Iterator iter = result.iterator(); iter.hasNext();) {
                FundAssetHelper helper = (FundAssetHelper) iter.next();
                logger.debug("WTF 'FundAsset id' returns: " + helper.getFundAssets().getId());
                logger.debug("WTF 'TotalSharesSum' returns: " + helper.getTotalSharesSum());
                Set <FundAssetTradeHistoryBO> wtfSet = helper.getFundAssets().getFundAssetTradeHistory();
                for (Iterator iter2 = wtfSet.iterator(); iter2.hasNext();) {
                    FundAssetTradeHistoryBO element = (FundAssetTradeHistoryBO) iter2.next();
                    logger.debug("WTF 'looping Shares: " + element.getShares());
                   
                }
            }           
        }


        return result;

    }


fath is a Set of FundAssetTradeHistory inside FundAsset. I want the sum of all shares that is on fath.

Debugging this, I'm not getting the sum I want, but rather, one of the values in the set sometimes, and numbers I don't understand other times. None of these results add up to the sum it should be:

Code:
WTF 'FundAsset id' returns: 7
WTF 'TotalSharesSum' returns: 90000.0
WTF 'looping Shares: 170000.0
WTF 'looping Shares: 160000.0
WTF 'looping Shares: 110000.0
WTF 'looping Shares: 70000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: -200000.0
WTF 'FundAsset id' returns: 7
WTF 'TotalSharesSum' returns: 70000.0
WTF 'looping Shares: 170000.0
WTF 'looping Shares: 160000.0
WTF 'looping Shares: 110000.0
WTF 'looping Shares: 70000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: -200000.0
WTF 'FundAsset id' returns: 7
WTF 'TotalSharesSum' returns: 110000.0
WTF 'looping Shares: 170000.0
WTF 'looping Shares: 160000.0
WTF 'looping Shares: 110000.0
WTF 'looping Shares: 70000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: -200000.0
WTF 'FundAsset id' returns: 7
WTF 'TotalSharesSum' returns: 160000.0
WTF 'looping Shares: 170000.0
WTF 'looping Shares: 160000.0
WTF 'looping Shares: 110000.0
WTF 'looping Shares: 70000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: -200000.0
WTF 'FundAsset id' returns: 7
WTF 'TotalSharesSum' returns: 170000.0
WTF 'looping Shares: 170000.0
WTF 'looping Shares: 160000.0
WTF 'looping Shares: 110000.0
WTF 'looping Shares: 70000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: 30000.0
WTF 'looping Shares: -200000.0
WTF 'FundAsset id' returns: 13
WTF 'TotalSharesSum' returns: -580000.0


Here's the Helper object in case it helps:

Code:
package com.wazollc.alphatheory.hibernate.dao;

import com.wazollc.alphatheory.hibernate.bo.FundAssetBO;

public class FundAssetHelper {
   
    private FundAssetBO fundAsset;
    Double totalSharesSum;
    public FundAssetHelper(FundAssetBO fundAsset, Double totalSharesSum) {
        // TODO Auto-generated constructor stub
        this.fundAsset = fundAsset;
        this.totalSharesSum = totalSharesSum;
    }
    public FundAssetBO getFundAssets() {
        return fundAsset;
    }
    public Double getTotalSharesSum() {
        return totalSharesSum;
    }

}


Any ideas?
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 10:06 am 
Beginner
Beginner

Joined: Fri Apr 15, 2005 3:30 pm
Posts: 46
Location: Fortaleza, Brazil
My bad - problem fixed. I put the aggregate function param in the 'group by' - when I took fath.shares out of the group by, it worked.

cheers,
Robert


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

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.