-->
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.  [ 2 posts ] 
Author Message
 Post subject: selectively eager fetching
PostPosted: Sun Oct 12, 2008 1:51 pm 
Newbie

Joined: Fri Jan 18, 2008 9:03 am
Posts: 2
I have a hibernate entity that represents a table row that maps as against itself.
@Entity
@Table(name = "blog_post", schema = "webapps")
public class BlogPost implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = -3645846346609906425L;
private Long blogPostId;
private BlogPost blogPost;
private Date postDate;
private String title;
private String content;
private String author;
private Set<BlogPost> blogPosts = new HashSet<BlogPost>(0);


@Id
@Column(name = "blog_post_id", unique = true, nullable = false)
public Long getBlogPostId() {
return this.blogPostId;
}

public void setBlogPostId(Long blogPostId) {
this.blogPostId = blogPostId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_post_id")
public BlogPost getBlogPost() {
return this.blogPost;
}

public void setBlogPost(BlogPost blogPost) {
this.blogPost = blogPost;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "post_date", nullable = false, length = 29)
public Date getPostDate() {
return this.postDate;
}

public void setPostDate(Date postDate) {
this.postDate = postDate;
}

@Column(name = "title")
public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

@Column(name = "content", nullable = false)
public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

@Column(name = "author", length = 256)
public String getAuthor() {
return this.author;
}

public void setAuthor(String author) {
this.author = author;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "blogPost")
public Set<BlogPost> getBlogPosts() {
return this.blogPosts;
}

public void setBlogPosts(Set<BlogPost> blogPosts) {
this.blogPosts = blogPosts;
}


}

In most cases, I do not want to hidrate the Set of BlogPost objects. So I do not want the Set to be eagerly fetched except when I want it. How do I selectively trigger eager fetching in either criteria or hql?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 6:37 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Keep the associations lazy, and indicate eager fetching specifically on those Queries or HQL you want.

In Criteria:
Code:
User user = (User) session.createCriteria(User.class)
                .setFetchMode("permissions", FetchMode.JOIN)
                .add( Restrictions.idEq(userId) )
                .uniqueResult();


in HQL:

Code:
from Sale sale
where sale.date > :startDate
left join fetch sale.product

_________________
Gonzalo Díaz


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