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: pull data from two tables into a single list correct order
PostPosted: Wed Jan 03, 2007 11:53 am 
Newbie

Joined: Wed Jan 03, 2007 11:41 am
Posts: 2
Hi,

I am trying to pull data from two tables into a single list, how can I get the total result ordered correctly so that it orders the full result set. Currently, when I run my query, the data comes back in the correct order for each of the individual queries issued by hibernate, but not as a single correctly ordered list.

To explain slightly better,

I have archived my transaction data into a separate database using a datbase link. So I now have current transactions in my operations database, and the archived transactions in a separate database that is accessed through a view on the operations database.

I have two classes the transaction class with the appropriate detail
and the archived_transaction class which extends the transaction class, with no additional fields or data in it.

I have two hbm files, with identical mappings except that one goes to the transaction table, and the other to the archived transaction view - see below.

Now when I issue my get transactions call

Code:
   List<Transaction> transactionList   = getHibernateTemplate().find("from com.myproject.dataobject.Transaction t order by t.batch.name, t.id");


the hibernate sql that is generated and run is:-

Code:
Hibernate: select archivedtr0_.TRANSACTION_ID as TRANSACT1_4_0_, archivedba1_.batch_id as batch1_3_1_, archivedtr0_.BATCH_ID as BATCH2_4_0_, archivedtr0_.AMOUNT as AMO7_4_0_, archivedba1_.name as name4_3_1_from ARCHIVED_TRANSACTION archivedtr0_ left outer join archived_batch archivedba1_ on archivedtr0_.BATCH_ID=archivedba1_.batch_id order by archivedba1_.merchant_name, archivedtr0_.BATCH_ID, archivedtr0_.TRANSACTION_ID
Hibernate: select transactio0_.TRANSACTION_ID as TRANSACT1_41_0_, batch1_.batch_id as batch1_8_1_,  transactio0_.BATCH_ID as BATCH2_41_0_, transactio0_.AMOUNT as AMO7_41_0_, batch1_.name as name4_8_1_ from TRANSACTION transactio0_ left outer join batch batch1_ on transactio0_.BATCH_ID=batch1_.batch_id order by batch1_.name, transactio0_.BATCH_ID, transactio0_.TRANSACTION_ID


Is there anyway to specify that
1) I want the whole result set from the two queries to be ordered and not the results within each query.
and
2) I want the second query run first (ie the live data before the archive data).

Any help will be greatly appreciated.
Code:
<hibernate-mapping package="com.myproject.dataobject">
    <class name="Transaction" table="TRANSACTION">
        <id
            column="TRANSACTION_ID"
            name="id"
            type="long"
            >
            <generator class="native" />
        </id>
        <many-to-one name="batch"
            not-null="true"
            class="com.myproject.dataobject.Batch"
            cascade="save-update"
            >
            <column name="BATCH_ID" />
        </many-to-one>
        <property
            column="AMOUNT"
            length="20"
            name="smount"
            not-null="false"
            type="big_decimal"
            />
    </class>
</hibernate-mapping>
       
<hibernate-mapping package="com.myproject.dataobject">
    <class name="ArchiveTransaction" table="ARCHIVE_TRANSACTION">
        <id
            column="TRANSACTION_ID"
            name="id"
            type="long"
            >
            <generator class="native" />
        </id>
        <many-to-one name="batch"
            not-null="true"
            class="com.myproject.dataobject.ArchiveBatch"
            cascade="save-update"
            >
            <column name="BATCH_ID" />
        </many-to-one>
        <property
            column="AMOUNT"
            length="20"
            name="smount"
            not-null="false"
            type="big_decimal"
            />
    </class>
</hibernate-mapping>
       



Regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 6:16 pm 
Newbie

Joined: Wed Aug 03, 2005 2:13 am
Posts: 16
Hi,

If you want to do this at the DB level and not in Java collections, then it sounds like you need a union. I don't think HQL/criteria queries support those though.

Perhaps you could use inheritance mapping and <union-subclass>.

See here: http://www.hibernate.org/hib_docs/v3/reference/en/html/inheritance.html

You would have a single table per concrete class (transaction and archived_transaction), but they could have a common parent class (BaseTransaction) that would let you do polymorphic queries.

ie- "from BaseTransaction order by ...".

hope that helps,
D.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 1:53 pm 
Newbie

Joined: Wed Jan 03, 2007 11:41 am
Posts: 2
I do not think it is possible to do at the database level, what I was hoping was that it would be done (by magic) at the hibernate level.

Thanks for your response and suggestion, though I think I am going to have to develop a sorter for the results.


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.