-->
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.  [ 3 posts ] 
Author Message
 Post subject: Derived Property using Formula
PostPosted: Sat Sep 15, 2007 9:20 pm 
Newbie

Joined: Wed Jun 21, 2006 2:25 pm
Posts: 15
hi all,

I am having issues in defining a formula for a Entity attribute. Basically there are 2 entities Account and TransactionRecord. On the Account entity I am trying to define a derived property "creditBalance" which will fetch the sum of all Transaction amounts.

Code:
<hibernate-mapping default-cascade="none">
    <class name="org.thoughtwok.cash4j.domain.AccountImpl" table="ACCOUNT" dynamic-insert="false" dynamic-update="false">
        <id name="id" type="java.lang.Long" unsaved-value="null">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="native">
            </generator>
        </id>
        <property name="accountNumber" >
            <column name="ACCOUNT_NUMBER" not-null="true" unique="false" sql-type="VARCHAR(256)"/>
            <type name="java.lang.String">
            </type>
        </property>
        <property name="creditBalance">
            <formula>select sum(tr.amount) from org.thoughtwok.cash4j.domain.TransactionRecordImpl tr where tr.creditAccount.id = id </formula>
        </property>
        <set name="creditTransactions" order-by="CREDIT_ACCOUNT_FK" lazy="true" fetch="select" inverse="true">
            <key foreign-key="TRANSACTION_RECORD_CREDIT_ACCC">
                <column name="CREDIT_ACCOUNT_FK" sql-type="BIGINT"/>
            </key>
            <one-to-many class="org.thoughtwok.cash4j.domain.TransactionRecordImpl"/>
        </set>
        <set name="debitTransactions" order-by="DEBIT_ACCOUNT_FK" lazy="true" fetch="select" inverse="true">
            <key foreign-key="TRANSACTION_RECORD_DEBIT_ACCOC">
                <column name="DEBIT_ACCOUNT_FK" sql-type="BIGINT"/>
            </key>
            <one-to-many class="org.thoughtwok.cash4j.domain.TransactionRecordImpl"/>
        </set>
    </class>
</hibernate-mapping>


Code:
<hibernate-mapping default-cascade="none">
    <class name="org.thoughtwok.cash4j.domain.TransactionRecordImpl" table="TRANSACTION_RECORD" dynamic-insert="false" dynamic-update="false">
        <id name="id" type="java.lang.Long" unsaved-value="null">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="native">
            </generator>
        </id>
        <property name="amount" >
            <column name="AMOUNT" not-null="true" unique="false" sql-type="DOUBLE"/>
            <type name="java.lang.Double">
            </type>
        </property>
        <many-to-one name="creditAccount" class="org.thoughtwok.cash4j.domain.AccountImpl" foreign-key="TRANSACTION_RECORD_CREDIT_ACCC" lazy="proxy" fetch="select">
            <column name="CREDIT_ACCOUNT_FK" not-null="true" sql-type="BIGINT"/>
        </many-to-one>
        <many-to-one name="debitAccount" class="org.thoughtwok.cash4j.domain.AccountImpl" foreign-key="TRANSACTION_RECORD_DEBIT_ACCOC" lazy="proxy" fetch="select">
            <column name="DEBIT_ACCOUNT_FK" not-null="true" sql-type="BIGINT"/>
        </many-to-one>
    </class>
</hibernate-mapping>


The Account.hbm.xml has a declaration
Code:
<property name="creditBalance">
            <formula>select sum(tr.amount) from org.thoughtwok.cash4j.domain.TransactionRecordImpl tr where tr.creditAccount.id = id </formula>
        </property>


For some reason this is generating sql queries such as
Code:
select accountimp0_.ID as ID0_0_, accountimp0_.ACCOUNT_NUMBER as ACCOUNT2_0_0_, accountimp0_.DESCRIPTION as DESCRIPT3_0_0_, accountimp0_.CREATED_ON as CREATED4_0_0_, accountimp0_.ACCOUNT_TYPE as ACCOUNT5_0_0_, accountimp0_.ACCOUNT_STATUS as ACCOUNT6_0_0_, select sum(tr.amount) from org.thoughtwok.cash4j.domain.TransactionRecordImpl tr where tr.creditAccount.id = accountimp0_.id  as formula0_0_  from ACCOUNT accountimp0_ where accountimp0_.ID=?


The above sql query shows the issue.
    select sum(tr.amount) from org.thoughtwok.cash4j.domain.TransactionRecordImpl tr where tr.creditAccount.id = accountimp0_.id as formula0_0_ is appearing as part of the larger select statement and not as a subquery
    hibernate is still using the entity name rather than the database table name


I believe (and the documentation says so) that such values will be determined using subqueries. This is the not the case.

Can someone please help in figuring out how to use the formula element here effectively. Any help would be appreciated

Thanks
anand raman


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 15, 2007 11:34 pm 
Newbie

Joined: Wed Jun 21, 2006 2:25 pm
Posts: 15
I think I found out what the issue was. The formula column expects verbatim sql statements. I was using all kinds of permutations of the actual hibernate entities and hence predicatabily there was no outcome.

4 hours of wasted effort or 4 hours of life long learning ??

Thanks
anand


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 16, 2007 11:49 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Definitely 4 hours of learning Anand :) But yes, Formulas do take SQL rather than HQL since the formula value is written directly into the SQL query that Hibernate generates. It does affect portability somewhat, but still a very effective solution.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.