Avoiding secondary selects or joins with Hibernate Criteria or HQL query
Posted
by Ben Benson
on Stack Overflow
See other posts from Stack Overflow
or by Ben Benson
Published on 2010-05-29T11:08:56Z
Indexed on
2010/05/29
11:12 UTC
Read the original article
Hit count: 312
I am having trouble optimizing Hibernate queries to avoid performing joins or secondary selects.
When a Hibernate query is performed (criteria or hql), such as the following:
return getSession().createQuery(("from GiftCard as card where card.recipientNotificationRequested=1").list();
... and the where clause examines properties that do not require any joins with other tables... but Hibernate still performs a full join with other tables (or secondary selects depending on how I set the fetchMode).
The object in question (GiftCard) has a couple ManyToOne associations that I would prefer to be lazily loaded in this case (but not necessarily all cases). I want a solution that I can control what is lazily loaded when I perform the query.
Here's what the GiftCard Entity looks like:
@Entity
@Table(name = "giftCards")
public class GiftCard implements Serializable
{
private static final long serialVersionUID = 1L;
private String id_;
private User buyer_;
private boolean isRecipientNotificationRequested_;
@Id
public String getId()
{
return this.id_;
}
public void setId(String id)
{
this.id_ = id;
}
@ManyToOne
@JoinColumn(name = "buyerUserId")
@NotFound(action = NotFoundAction.IGNORE)
public User getBuyer()
{
return this.buyer_;
}
public void setBuyer(User buyer)
{
this.buyer_ = buyer;
}
@Column(name="isRecipientNotificationRequested", nullable=false, columnDefinition="tinyint")
public boolean isRecipientNotificationRequested()
{
return this.isRecipientNotificationRequested_;
}
public void setRecipientNotificationRequested(boolean isRecipientNotificationRequested)
{
this.isRecipientNotificationRequested_ = isRecipientNotificationRequested;
}
}
© Stack Overflow or respective owner