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.  [ 6 posts ] 
Author Message
 Post subject: sql-query and load-collection -- how?
PostPosted: Fri May 13, 2005 3:11 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
I have a data model which has three tables: STORE, REGISTER, and PURCHASE. A store has many registers and a register has many purchases. In my object model I need to create a collection of the 100 most recent purchases on the Store class. I figured using a set and the loader/sql-query elements would do the trick, but there's not a whole lot of documentation on how to use these elements. Here's what I've got so far:

Code:
<class name="Store" table="STORE">
  <id name="id" column="STORE_ID" type="integer"/>
  <set name="recentPurchases">
    <key column="STORE_ID"/>
    <one-to-many class="Purchase"/>
    <loader query-ref="recentPurchasesQuery"/>
  </set>
</class>

<sql-query name="recentPurchasesQuery">
  <load-collection alias="p" role="Store.recentPurchases"/>
  select {p.*}
  from register r, purchase p
  where r.store_id = :id
  and r.register_id = p.register_id
  order by p.purchase_datetime desc
  limit 100
</sql-query>


This results in the following generated SQL:

Code:
select      p.STORE_ID as STORE6___,
            p.PURCHASE_ID as PURCHASE1___,
            p.PURCHASE_ID as PURCHASE1_0_,
            p.REGISTER_ID as REGISTER2_7_0_,
            p.EMPLOYEE_ID as EMPLOYEE3_7_0_,
            p.CUSTOMER_ID as CUSTOMER4_7_0_,
            p.PURCHASE_DATETIME as PURCHASE5_7_0_,
            ('Purchase ' || p.PURCHASE_ID) as formula3_0_
from        register r,
            purchase p
where       r.store_id = ? and
            r.register_id = p.register_id
order by    p.purchase_datetime desc
limit       100


The first column in the select is problematic because it doesn't exist on the PURCHASE table (but it is on the REGISTER table). Also, I don't understand the purpose/mechanics of the {p.*} construct. Furthermore, am I supposed to retrieve all the columns that are needed to construct the instance, or can I simply retrieve the primary keys and use that to drive the instantiation of the purchase objects?

Any help, pointers to documentation, etc, is greatly appreciated!

Thanks,

-Kaare


Top
 Profile  
 
 Post subject: Re: sql-query and load-collection -- how?
PostPosted: Fri May 13, 2005 3:45 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kaared wrote:
I have a data model which has three tables: STORE, REGISTER, and PURCHASE. A store has many registers and a register has many purchases. In my object model I need to create a collection of the 100 most recent purchases on the Store class. I figured using a set and the loader/sql-query elements would do the trick, but there's not a whole lot of documentation on how to use these elements. Here's what I've got so far:

Code:
<class name="Store" table="STORE">
  <id name="id" column="STORE_ID" type="integer"/>
  <set name="recentPurchases">
    <key column="STORE_ID"/>
    <one-to-many class="Purchase"/>
    <loader query-ref="recentPurchasesQuery"/>
  </set>
</class>

<sql-query name="recentPurchasesQuery">
  <load-collection alias="p" role="Store.recentPurchases"/>
  select {p.*}
  from register r, purchase p
  where r.store_id = :id
  and r.register_id = p.register_id
  order by p.purchase_datetime desc
  limit 100
</sql-query>


This results in the following generated SQL:

Code:
select      p.STORE_ID as STORE6___,
            p.PURCHASE_ID as PURCHASE1___,
            p.PURCHASE_ID as PURCHASE1_0_,
            p.REGISTER_ID as REGISTER2_7_0_,
            p.EMPLOYEE_ID as EMPLOYEE3_7_0_,
            p.CUSTOMER_ID as CUSTOMER4_7_0_,
            p.PURCHASE_DATETIME as PURCHASE5_7_0_,
            ('Purchase ' || p.PURCHASE_ID) as formula3_0_
from        register r,
            purchase p
where       r.store_id = ? and
            r.register_id = p.register_id
order by    p.purchase_datetime desc
limit       100


The first column in the select is problematic because it doesn't exist on the PURCHASE table (but it is on the REGISTER table). Also, I don't understand the purpose/mechanics of the {p.*} construct. Furthermore, am I supposed to retrieve all the columns that are needed to construct the instance, or can I simply retrieve the primary keys and use that to drive the instantiation of the purchase objects?

Any help, pointers to documentation, etc, is greatly appreciated!

Thanks,

-Kaare


You shouldn't be so quick to use sql-queries. Hibernate will do most of these things pretty effortlessly.

Code:
List firstHundredPurchasesByStore =
  session.createCriteria(Purchase.class)
            .add(Restrictions.eq("storeId",storeId)
            .addOrder(Order.desc("purchaseDateTime"))
            .setMaxResults(100)
            .list();


You example query indicates that even though a purchase belongs to a register, it does contain a store id, so unless you want to break these down by register, the register table isn't necessary for this query.


Top
 Profile  
 
 Post subject: Re: sql-query and load-collection -- how?
PostPosted: Fri May 13, 2005 3:59 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
pksiv wrote:
You shouldn't be so quick to use sql-queries. Hibernate will do most of these things pretty effortlessly.

Code:
List firstHundredPurchasesByStore =
  session.createCriteria(Purchase.class)
            .add(Restrictions.eq("storeId",storeId)
            .addOrder(Order.desc("purchaseDateTime"))
            .setMaxResults(100)
            .list();




Unfortunately this requires that code is written. I'm using Hibernate as part of a framework and would like to have this map-driven. By the way, why shouldn't I be using sql-query instead of the API? Is the performance worse with the map?

pksiv wrote:
You example query indicates that even though a purchase belongs to a register, it does contain a store id, so unless you want to break these down by register, the register table isn't necessary for this query.


The query was generated by Hibernate, not me. The query is incorrect since it states that there's a STORE_ID on the PURCHASE table when only the REGISTER table has the STORE_ID (hence the join).

Any idea on how to do this using a mapping?

Thanks,

-Kaare


Top
 Profile  
 
 Post subject: Re: sql-query and load-collection -- how?
PostPosted: Fri May 13, 2005 4:10 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kaared wrote:

Unfortunately this requires that code is written. I'm using Hibernate as part of a framework and would like to have this map-driven. By the way, why shouldn't I be using sql-query instead of the API? Is the performance worse with the map?

The query was generated by Hibernate, not me. The query is incorrect since it states that there's a STORE_ID on the PURCHASE table when only the REGISTER table has the STORE_ID (hence the join).

Any idea on how to do this using a mapping?

Thanks,

-Kaare


Not sure what you mean by "using Hibernate as part of a framework". Hibernate is a framework and I can't see how you can even execute sql-queries without writing code.

I haven't used map-driven objects so I can't really offer any advice there.

All that I can say is I use hibernate with pojos, and the minimal code required for criteria queries cuts down tremendously on development time.


Top
 Profile  
 
 Post subject: Re: sql-query and load-collection -- how?
PostPosted: Fri May 13, 2005 4:26 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
pksiv wrote:
Not sure what you mean by "using Hibernate as part of a framework". Hibernate is a framework and I can't see how you can even execute sql-queries without writing code.

I'm using Hibernate as the data access layer in another framework. Anyway, executing SQL queries without writing code is what I think the purpose of the loader, sql-query, and load-collection elements is. Now if I can only figure out how they're supposed to work. :-) Anybody?

Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 7:48 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
You need to realise that the <key> element refers to a column on the many end of the association.

i.e. your mapping specifies the key column on Purchase is called STORE_ID:

<set name="recentPurchases">
<key column="STORE_ID"/>
<one-to-many class="Purchase"/>

Ben


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