Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Indexing the last element in a collection
PostPosted: Mon May 28, 2012 3:32 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 208
Hi,

I have the following setup
Code:
@OneToMany(cascade={ CascadeType.ALL },orphanRemoval=true,mappedBy="post",fetch=FetchType.LAZY)
@BatchSize(size = 10)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region=CacheRegion.WALL)
@Field(name="placeholder")
@FieldBridge(impl=CommentFieldBridge.class)
@OrderBy
public Set<Comment> getComments() {
   return comments;
}


This fieldbridge is responsible for indexing the last element in the Set of comments with the Post.

Code:
public class CommentFieldBridge implements FieldBridge{

   @Override
   public void set(String name, Object value, Document document, LuceneOptions options) {
      Set<Comment> comments = (Set<Comment>) value;
        }


First a few observations:
1. All the comments are being loaded in the field bridge, regardless of the setting BatchSize. It would make more sense to me to preload the @BatchSize and load the rest on demand. I'm wondering: what if there are 10.000 comments? I'm also wondering if this behaviour (loading all children when a new child is added) is normal or caused by the use of the FieldBridge?

Then the question: how to best implement this in a way that is performant and scalable? Obviously, loading all comments by default in the Fieldbridge is neither.
* I was thinking of adding an extra field to the Post class.
Code:
Set<Comment> Comments = new HashSet<Comment>();
Comment lastComment;

public void addComments(Comment comment){
   comments.add(comment);
   lastComment = comment;
}

@ManyToOne
public Comment getLastComment(){
   return lastComment;
}


* I'm not too sure what would be the best setting for cascading with a combination of a @ManyToOne and a @OneToMany that point to the same object.

Any suggestions?

Kind regards,

Marc


Top
 Profile  
 
 Post subject: Re: Indexing the last element in a collection
PostPosted: Tue May 29, 2012 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
Hi Marc,
since it's a Set it's expected that you want to load all comments as soon as you trigger initialization of the collection via any method on it.

You might be wanting an ordered collection instead (such as List) or maybe a Map if you would have a suitable object for the keys of the map.
Also, have a look at mapping options such as "extra-lazy" and different fetching strategies: http://docs.jboss.org/hibernate/core/4. ... e-fetching

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing the last element in a collection
PostPosted: Wed May 30, 2012 12:38 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 208
Thanks Sanne,

That makes sense. I've read a lot about Sets and Lists and Hibernate. Stuff like, if you use a List, any change to it will lead to deletion of all the List items and reinsertion, which didn't make sense to me, so I tended towards Sets, which seemed a more natural way to express many things anyways.
But this extra-lazy stuff looks great. Wonder why it isn't the default.

Cheers,
Marc


Top
 Profile  
 
 Post subject: Re: Indexing the last element in a collection
PostPosted: Wed May 30, 2012 1:13 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
Quote:
Wonder why it isn't the default.


because there are many use cases in which fetching all at once is more efficient. It likely is, unless you expect large collections.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.