-->
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.  [ 7 posts ] 
Author Message
 Post subject: lazy collection is fetched eagerly
PostPosted: Tue Feb 05, 2008 4:43 pm 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
Hibernate version: Core 3.2.5.ga, EntityManager 3.3.1.ga

Entities:

@Entity
@Proxy
public class Product implements Serializable {
private Long id;
private String name;

public Product()
{
}

public Product(String name)
{
setName(name);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}

@Override
public int hashCode() {
int hash = 0;
hash += (getId() != null ? getId().hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Product)) {
return false;
}
Product other = (Product) object;
if ((this.getId() == null && other.getId() != null)
|| (this.getId() != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "Product[id=" + getId() + "]";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setId(Long id) {
this.id = id;
}

}

@Entity
@Proxy
public class Store implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Set<Product> products;

public Store()
{
}

public Store(Collection<Product> products)
{
getProducts().addAll(products);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}

@Override
public int hashCode() {
int hash = 0;
hash += (getId() != null ? getId().hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// not set
if (!(object instanceof Store)) {
return false;
}
Store other = (Store) object;
if ((this.getId() == null && other.getId() != null)
|| (this.getId() != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "Store[id=" + getId() + "]";
}

@ManyToMany(fetch=FetchType.LAZY)
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<Product> getProducts() {
if (products == null)
{
products = new HashSet<Product>();
}
return products;
}

public void setProducts(Set<Product> products) {
this.products = products;
}

public void setId(Long id) {
this.id = id;
}
}


Name and version of the database you are using: HSQLDB 1.8.0.7

I create a Store instance with many products in it.
When I call Store.getProducts() it is loaded eagerly:

select
products0_.Store_id as Store1_1_,
products0_.products_id as products2_1_,
product1_.id as id3_0_,
product1_.name as name3_0_
from
Store_Product products0_
left outer join
Product product1_
on products0_.products_id=product1_.id
where
products0_.Store_id=?

The result is correct but I expected lazy fetching...
Am I missing something?

Thanks in advance!
Norbi


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Tue Feb 05, 2008 5:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Extra lazy works only for operations that do not need the whole collection. If you do something like size or similar then it would trigger initializing the whole collection.



Farzad-


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Wed Feb 06, 2008 4:12 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
farzad wrote:
Extra lazy works only for operations that do not need the whole collection. If you do something like size or similar then it would trigger initializing the whole collection.


Thanks for your reply.
I only call getProducts() then iterate through the collection, so I would not expect eager fetching. I tried LazyCollectionOption.EXTRA and LazyCollectionOption.TRUE as well but none of them works lazily.


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Wed Feb 06, 2008 10:52 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Sorry my bad. I gave you quite wrong information. Extra lazy doesn't do much on sets. It is actually quite useful only on the size method which triggers a select count on the database. However, iterating over a set will instantiate the whole collection. On maps and lists, extra lazy also enables fetching an entry by index. Still iterating over a list or a map will fetch the whole collection from database.



Farzad-


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Thu Feb 07, 2008 2:51 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
farzad wrote:
On maps and lists, extra lazy also enables fetching an entry by index. Still iterating over a list or a map will fetch the whole collection from database.


I changed Set to List but it still fetches eagerly... :(
The problem is that it still fetches the entire collection when I call getProducts(), so when I access an entry by index the entire collection is loaded already.


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Thu Feb 07, 2008 12:32 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Weird. It works for me. Send me your new mapping and entity class and I will take a look at it. Also show me the code you use for testing. Please use code tags so that it will be easier for me to read it here.



Farzad-


Top
 Profile  
 
 Post subject: Re: lazy collection is fetched eagerly
PostPosted: Fri Feb 08, 2008 4:24 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
farzad wrote:
Weird. It works for me. Send me your new mapping and entity class and I will take a look at it. Also show me the code you use for testing. Please use code tags so that it will be easier for me to read it here.


I don't know what did I change in my code, but now it works.
Extra lazy mode is working as well, I tried it for Lists. (I didn't know that it is intended only for Lists and Maps, maybe Hibernate should warn me about it when I annotate a Set as extra lazy.)

Thanks for your help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.