|
View:
New views
1 Messages
—
Rating Filter:
Alert me
|
|
|
JPA & Hibernate: Does a Query not return a proxy collection ?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. So in case you have many customers it would get slow or even produces an out of memory error... I am quite sure that here something exists how to avoid this kind of situation. 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/persistence_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 |
| Free embeddable forum powered by Nabble | Forum Help |