-->
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: Hibernate executes too many queries...
PostPosted: Fri Apr 01, 2011 11:11 am 
Newbie

Joined: Thu Mar 31, 2011 4:10 pm
Posts: 3
Hi, I'm working for the first time with Hibernate and I'm having some query problems.

When I execute a simple retrieve with my DAO :
Code:
@Override
   public SortedSet<Jurisdiction> getAllSortedWithLocalized() {
      SortedSet<Jurisdiction> results = new TreeSet<Jurisdiction>();
      Query query = this.session.createQuery(
            "from " + Jurisdiction.class.getName() + " jurisdiction " +
            "left join fetch jurisdiction.localizedFields ");
      
      results.addAll(query.list());
      return results;
   }


Hibernate executes the following queries :

Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, localizedf1_.locale as locale1_1_, localizedf1_.jur_id as jur5_1_1_, jurisdicti0_.rank as rank0_0_, localizedf1_.title as title1_1_, localizedf1_.url_image as url3_1_1_, localizedf1_.url_image_target as url4_1_1_, localizedf1_.jur_id as jur5_0_0__, localizedf1_.locale as locale0__, localizedf1_.jur_id as jur5_0__ from jurisdiction jurisdicti0_ left outer join jurisdiction_localized localizedf1_ on jurisdicti0_.jur_id=localizedf1_.jur_id
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?
Hibernate: select jurisdicti0_.jur_id as jur1_0_0_, jurisdicti0_.rank as rank0_0_ from jurisdiction jurisdicti0_ where jurisdicti0_.jur_id=?


Now, the first executed query retrieves everything I need... all of the jurisdiction and their localized fields. The problem is after the first query, Hibernate executes a SELECT for EVERY jurisdiction in the database. I can't figure out why, since those fields are already retrieved with the first query.

Here is my entity class (minus all getters/setters) :
Code:
@Entity
@Table(name = "jurisdiction")
public class Jurisdiction implements Comparable<Jurisdiction>{
   
   @Column(name = "jur_id")
   @Id
   private String jurId;

   private Integer rank;
   
   @Sort(type=SortType.NATURAL)
   @OneToMany(mappedBy="jurisdiction", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   private SortedSet<FrequentlyConsultedDocument> frequentlyConsultedDocuments = new TreeSet<FrequentlyConsultedDocument>();
   
   @Sort(type=SortType.NATURAL)
   @OneToMany(mappedBy="jurisdiction", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   private SortedSet<ExternalLink> externalLinks = new TreeSet<ExternalLink>();

   @Sort(type=SortType.NATURAL)
   @OneToMany(mappedBy="jurisdiction", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   @Where(clause="collection_type='COURT'")
   private SortedSet<Collection> courts = new TreeSet<Collection>();
   
   @Sort(type=SortType.NATURAL)
   @OneToMany(mappedBy="jurisdiction", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   @Where(clause="collection_type='TRIBUNAL'")
   private SortedSet<Collection> tribunals = new TreeSet<Collection>();
   
   @OneToMany(targetEntity=JurisdictionLocalizedFields.class, mappedBy="jurisdiction", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
   @MapKeyColumn(name="locale")
   private Map<Locale, JurisdictionLocalizedFields> localizedFields = new HashMap<Locale, JurisdictionLocalizedFields>(2);
   
   
   public Jurisdiction(){}

   public Jurisdiction(String jurId, int rank) {
      this.jurId = jurId;
      this.rank = rank;
   }
   
   @Entity
   @Table(name = "jurisdiction_localized")
   public static class JurisdictionLocalizedFields implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      Locale locale;
      
      String title;

      @Column(name = "url_image")
      String urlImage;
      
      @Column(name = "url_image_target")
      String urlImageTarget;
      
      @ManyToOne
      @JoinColumn(name="jur_id")
      @Id Jurisdiction jurisdiction;
      
      public JurisdictionLocalizedFields(){}
      
      public JurisdictionLocalizedFields(Locale locale, Jurisdiction jurisdiction, String title, String urlImage, String urlImageTarget){
         this.jurisdiction = jurisdiction;
         this.locale = locale;
         this.urlImage = urlImage;
         this.urlImageTarget = urlImageTarget;
      }
         }


I am using the Hibernate version 3.6.1.Final with the Postgresql dialect.

Any help is appreciated.

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate executes too many queries...
PostPosted: Mon Apr 04, 2011 10:56 am 
Newbie

Joined: Thu Mar 31, 2011 4:10 pm
Posts: 3
I still did not find any solution and I've been on this problem for some days now... anything would be very helpful.


Top
 Profile  
 
 Post subject: Re: Hibernate executes too many queries...
PostPosted: Tue Apr 05, 2011 10:28 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
Not sure if this is related to your issue or not... but I noticed you have jurisdiction annotated with @Id on the many side of your one-to-many from Jurisdiction to JurisdictionLocalizedFields (maybe that's due to the fact that you're using a Map - hard to say because I've never actually needed to "map" a relationship in that way). Also seems a little odd using the static modifier on JurisdictionLocalizedFields... I'd try separating that into a top level class rather than embedding it within Jurisdiction. The mapping for courts and tribunals also seems a little puzzling... can't say I've mapped many collections of type Collection (or any for that matter). I'd expect those to be declared as Set<Court> and Set<Tribunal> but it's possible I'm missing something.


Top
 Profile  
 
 Post subject: Re: Hibernate executes too many queries...
PostPosted: Wed Apr 06, 2011 2:20 pm 
Newbie

Joined: Thu Mar 31, 2011 4:10 pm
Posts: 3
You were right burro, that @Id on the many side of my relation was causing all those queries to execute. I tried with a generated value as id and it was all good!

I was about to give up on that bug and switch my DAOs to a JDBC implementation. Thanks to you I'll stay with Hibernate :D

Thanks alot for your time!


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.