-->
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.  [ 8 posts ] 
Author Message
 Post subject: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Sat Oct 17, 2009 9:57 am 
Newbie

Joined: Sat Oct 17, 2009 9:50 am
Posts: 4
Hello

I am using JPA with Hibernate (v3.3.1.). Something astonsihes me. When I do a query I noticed that the returned list is a normal "java.util.ArrayList". I expected a kind of proxy collection so that it supports a "lazy loading" (loading of the list elements itself at run time when needed) means if I would later iterate over the list that hibernate fills the list on demand.

Query query = entityManager.createQuery ( "from Customer" );
query.setHint("org.hibernate.fetchSize", 10);
log.info("returned type of getResultList(): " + query.getResultList().getClass());
List list = query.getResultList();
for(Object c : list) System.out.println(c);

this prints out the following type for the "ResultList" class:

[StandardQueryCache] starting query cache at region: regionname
[AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Hibernate: select customer0_.customer_id as customer1_1_, customer0_.customer_type as customer2_1_, customer0_.gender as gender1_, customer0_.last_name as last4_1_, customer0_.given_name as given5_1_, customer0_.company_name as company6_1_, customer0_.cust_domicile as cust7_1_, customer0_.street_address as street8_1_, customer0_.zip_code as zip9_1_, customer0_.city as city1_, customer0_.corres_code as corres11_1_, customer0_.domicile_Status as domicile12_1_, customer0_.advisor_branch_code as advisor13_1_, customer0_.advisor_dept_code as advisor14_1_, customer0_.cust_form as cust15_1_, customer0_.cust_category as cust16_1_, customer0_.cust_grade as cust17_1_, customer0_.cust_segment as cust18_1_, customer0_.civil_status as civil19_1_, customer0_.nationality1 as nationa20_1_, customer0_.nationality_status as nationa21_1_ from DTN_CUSTOMER customer0_
[AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
[AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)
[AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)

returned type of getResultList(): class java.util.ArrayList

Customer [id=1-0000-0001, lastName=xxx, givenName=xxx, birthDate=Sat Oct 17 14:23:14 CEST 2009]
Customer [id=1-0000-0002, lastName=yyy, givenName=yyy, city=Wetzikon, birthDate=Sat Oct 17 14:23:14 CEST 2009]
Customer [id=1-0000-0003, lastName=zzz, givenName=zzz, city=Hinwil, birthDate=Sat Oct 17 14:23:14 CEST 2009]
....

I expected that when I iterate over the 11th customer then I would notice another sql fetch but obviously this seems not to be case. Rather than this it gets all customers of the database.

The question is now for me, does a proxy collection for hibernate & JPA exist in case of queries? If yes please provide a link or example how the configuration therefore needs to be made. I did not found any kind of hints so far, only in case if you have sub attributes (one-to- many etc.)

Here my snippet code how I use it today.

Annotations of my Customer class:

@Entity
@Table (name="DTN_CUSTOMER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Customer implements Serializable {.... }

JPA persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="pbm">
<class>entity.Customer</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="net.sf.ehcache.configurationResourceName" value="hibernate-ehcache.xml"/>
</properties>
</persistence-unit>
</persistence>


If you need more information, please let me know. Any hints are welcome...

best regards
Mark


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Sat Oct 17, 2009 10:12 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi megloff,

why should the returned list not be of type java.util.ArrayList? That's not the point. The items within the list may be proxies depending on your mapping. Since you just query a bunch of entities that match your restrictions these have their standard properties initialized. You may find entities and collections associated to the fetched items being proxied depending on your mapping.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Sat Oct 17, 2009 1:11 pm 
Newbie

Joined: Sat Oct 17, 2009 9:50 am
Posts: 4
thank you Froestel

However, for me it is the point. Because now I have to think how I should avoid that not too many records at once are loaded from the DB. So I thought there is a way without using an explicit pagination.

regards
Mark


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Sun Oct 18, 2009 4:37 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi megloff,

you could probably use bytecode instrumentation to lazy load properties of entities. That might save you some unnecessary data from being loaded.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Fri Dec 04, 2009 7:51 am 
Newbie

Joined: Wed Feb 14, 2007 12:56 pm
Posts: 4
Froestel wrote:
you could probably use bytecode instrumentation to lazy load properties of entities. That might save you some unnecessary data from being loaded.


Could you give more details on this?

Thanks a lot,
Felipe


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Thu Jan 07, 2010 6:53 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi FjK,

Chap. 19 of the online Hibernate documentation provides some hints on bytecode instrumentation. I recommend you hack the term into your favourite search engine and check the results.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Fri Jan 08, 2010 4:15 am 
Newbie

Joined: Fri Jan 08, 2010 4:08 am
Posts: 4
Dynamic bytecode instrumentation is an innovative solution to these problems

_________________
craft ideas | india news


Top
 Profile  
 
 Post subject: Re: JPA Hibernate: Does a Query not return a proxy collection?
PostPosted: Fri Jan 08, 2010 7:49 am 
Newbie

Joined: Wed Feb 14, 2007 12:56 pm
Posts: 4
Froestel wrote:
Hi FjK,

Chap. 19 of the online Hibernate documentation provides some hints on bytecode instrumentation. I recommend you hack the term into your favourite search engine and check the results.

CU
Froestel


Thanks, but I already use lazy loading of properties. I'm actually looking for a solution as mentioned by megloff. I need lazy loading of rows. I'm currently using this solution http://www.ilikespam.com/blog/paging-la ... a-lazylist , but I'd like something simpler.

Isn't there any out-of-the-box solution for this?


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