-->
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.  [ 4 posts ] 
Author Message
 Post subject: Multiple queries being run where there should only be one
PostPosted: Tue May 31, 2011 6:28 pm 
Newbie

Joined: Tue May 31, 2011 4:38 pm
Posts: 2
I recently noticed that some of my team's older code is executing multiple queries when only one is necessary. It can be replicated by calling the Criteria.list or AbstractEntityManagerImpl.find functions for several of our tables, so it's likely something setup incorrectly on the entity. I think it has something to do with how we're doing multiple outer joins to the same table, which is mapped to multiple different entities.

Here's our setup:
Code:

@Table(name="problem")
public class ProblemEntity {
   @id
   @Column(name = "id")
   private long id;

   @OneToOne(fetch = FetchType.EAGER, optional = true)
   @JoinColumn(name = "comment_id")
   private CommentEntity comment;

   @OneToOne(fetch = FetchType.EAGER, optional = true)
   @JoinColumn(name = "description_id")
   private DescriptionEntity description;

   @OneToOne(fetch = FetchType.EAGER, optional = true)
   @JoinColumn(name = "instruction_id")
   private InstructionEntity instruction;
}

@Table(name="special_text")
public class CommentEntity {
   @id
   @Column(name = "id")
   private long comment_id;

   @Column(name = "text")
   private String comment;
}

@Table(name="special_text")
public class DescriptionEntity {
   @id
   @Column(name = "id")
   private long description_id;

   @Column(name = "text")
   private String description;
}

@Table(name="special_text")
public class InstructionEntity {
   @id
   @Column(name = "id")
   private long instruction_id;

   @Column(name = "text")
   private String instruction;
}


What I would expect from a basic search on ProblemEntity for a specific id would be a single query:
Code:
select p.*, c.*, d.*, i.*
from problem p
  left outer join special_text c on c.comment_id = p.comment_id
  left outer join special_text d on d.description_id = p.description_id
  left outer join special_text i on i.instruction_id = p.instruction_id
where p.id = ?


But instead, Hibernate generates the following queries:
Code:
select p.*, c.*, d.*, i.*
from problem p
  left outer join special_text c on c.comment_id = p.comment_id
  left outer join special_text d on d.description_id = p.description_id
  left outer join special_text i on i.instruction_id = p.instruction_id
where p.id = ?

select p.*, c.*, d.*, i.*
from problem p
  left outer join special_text c on c.comment_id = p.comment_id
  left outer join special_text d on d.description_id = p.description_id
  left outer join special_text i on i.instruction_id = p.instruction_id
where c.comment_id = ?

select p.*, c.*, d.*, i.*
from problem p
  left outer join special_text c on c.comment_id = p.comment_id
  left outer join special_text d on d.description_id = p.description_id
  left outer join special_text i on i.instruction_id = p.instruction_id
where d.description_id = ?

select p.*, c.*, d.*, i.*
from problem p
  left outer join special_text c on c.comment_id = p.comment_id
  left outer join special_text d on d.description_id = p.description_id
  left outer join special_text i on i.instruction_id = p.instruction_id
where i.instruction_id = ?


It's running the expected query first. Can anyone tell me why it's running the additional queries and how to prevent that from happening?


Top
 Profile  
 
 Post subject: Re: Multiple queries being run where there should only be one
PostPosted: Wed Jun 01, 2011 12:01 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
you are using special_text table for storing multiple type of objects but i don't see any discriminator there in your pasted configuration; so i guess that might be causing the problem


Top
 Profile  
 
 Post subject: Re: Multiple queries being run where there should only be one
PostPosted: Wed Jun 01, 2011 9:20 am 
Newbie

Joined: Wed Jun 01, 2011 9:01 am
Posts: 1
To expand on dan329's example (we're colleagues), the table itself does not know what type of object it is (i.e. there is no discriminator column). Functionally, all the entities are identical, the only knowledge of their difference belongs to the ProblemEntity. It knows that it only has 3 references to the special_text table in the form of comment, description, and instruction. In fact, we're actually using a MappedSuperclass for those three, only keeping the concretes for the purpose of the one-to-one relationship with the problem table.


Top
 Profile  
 
 Post subject: Re: Multiple queries being run where there should only be one
PostPosted: Tue Aug 02, 2011 5:51 pm 
Newbie

Joined: Tue May 31, 2011 4:38 pm
Posts: 2
It turns out that we had Hibernate configured to use the Field AccessType instead of Property. Unfortunately, we can't switch completely over to the Property AccessType since we're stuck on JPA1 and don't implement sure POJO entities.


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