-->
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: Question about lazy loading
PostPosted: Tue Dec 30, 2008 7:27 pm 
Newbie

Joined: Tue Dec 30, 2008 6:52 pm
Posts: 3
Hello, I have arrived at 13.1.2 of 'the book' which tells me lazy loading is Hibernate's default fetching strategy. However, when I load() or get() a new Section into the session like this

Section section = (Section) hsession.get(Section.class, new Long(1));

The generated SELECT statement joins multiple tables. It uses SECTION (good), but also SECTIONLAYOUT (referenced by Section) and even SECTIONTEMPLATE (referenced by SectionLayout). Why? Lazy loading as I understand it should prevent associated entities and collections from being retrieved untill it's actually necessary. I expected the query to just use the SECTION table.

I have tried setting the @org.hibernate.annotations.LazyToOne to prevent the join, but that doesn't change the generated SQL.

Is there something wrong with my mappings, my Hibernate setup, or with my understanding of lazy loading?

Thanks.
J


Hibernate version:
3.2.5.ga

Mapping documents:
Code:
@Entity
@Table(name = "SECTION")
public class Section  implements Serializable{
   
   private Long id;
   private String publishStatus;
   private String name;
   private String directoryPath;
   private String uniqueName;
   private SectionLayout sectionLayout;
   private Map<String, String> sectionProperties;
   private Set<SectionArticle> sectionArticles;
   private int leftValue;
   private int rightValue;
   
   public Section(){}

   @Id @GeneratedValue
   @Column(name="SECTION_ID")
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }

   @ManyToOne(targetEntity=model.SectionLayout.class)
   @JoinColumn(name = "SECTIONLAYOUT_ID")
   @org.hibernate.annotations.LazyToOne(
         org.hibernate.annotations.LazyToOneOption.PROXY
   )
   public SectionLayout getSectionLayout() {
      return sectionLayout;
   }
   public void setSectionLayout(SectionLayout sectionLayout) {
      this.sectionLayout = sectionLayout;
   }
   
   @org.hibernate.annotations.CollectionOfElements
   @JoinTable( name = "SECTIONPROPERTIES", joinColumns = @JoinColumn( name = "SECTION_ID" ))
   @org.hibernate.annotations.MapKey(columns = @Column( name = "PROPERTY" ))
   @Column( name = "VALUE")
   public Map<String, String> getSectionProperties() {
      return sectionProperties;
   }
   public void setSectionProperties(Map<String, String> sectionProperties) {
      this.sectionProperties = sectionProperties;
   }
   
   @OneToMany(mappedBy = "section")
   public Set<SectionArticle> getSectionArticles() {
      return sectionArticles;
   }
   public void setSectionArticles(Set<SectionArticle> sectionArticles) {
      this.sectionArticles = sectionArticles;
   }
   
   // ... etc

   
   
}



Code between sessionFactory.openSession() and session.close():
Code:
Section section = (Section) hsession.get(Section.class, new Long(1));

Full stack trace of any exception that occurs:
No Exception occurs
Name and version of the database you are using:
Mysql 5.0
The generated SQL (show_sql=true):
Code:
select
        section0_.SECTION_ID as SECTION1_4_2_,
        section0_.DIRECTORYPATH as DIRECTOR2_4_2_,
        section0_.LEFTVALUE as LEFTVALUE4_2_,
        section0_.NAME as NAME4_2_,
        section0_.PUBLISHSTATUS as PUBLISHS5_4_2_,
        section0_.RIGHTVALUE as RIGHTVALUE4_2_,
        section0_.SECTIONLAYOUT_ID as SECTIONL8_4_2_,
        section0_.UNIQUENAME as UNIQUENAME4_2_,
        sectionlay1_.SECTIONLAYOUT_ID as SECTIONL1_14_0_,
        sectionlay1_.name as name14_0_,
        sectionlay1_.SECTIONTEMPLATE_ID as SECTIONT3_14_0_,
        sectiontem2_.SECTIONTEMPLATE_ID as SECTIONT1_15_1_,
        sectiontem2_.NAME as NAME15_1_
    from
        SECTION section0_
    left outer join
        SECTIONLAYOUT sectionlay1_
            on section0_.SECTIONLAYOUT_ID=sectionlay1_.SECTIONLAYOUT_ID
    left outer join
        SECTIONTEMPLATE sectiontem2_
            on sectionlay1_.SECTIONTEMPLATE_ID=sectiontem2_.SECTIONTEMPLATE_ID
    where
        section0_.SECTION_ID=?



[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 7:54 pm 
Newbie

Joined: Tue Dec 30, 2008 6:52 pm
Posts: 3
I already solved it by setting the fetch = FetchType.LAZY annotation on the ManyToOne association to sectionLayout.

Resulting SQL
Code:
select
        section0_.SECTION_ID as SECTION1_4_0_,
        section0_.DIRECTORYPATH as DIRECTOR2_4_0_,
        section0_.LEFTVALUE as LEFTVALUE4_0_,
        section0_.NAME as NAME4_0_,
        section0_.PUBLISHSTATUS as PUBLISHS5_4_0_,
        section0_.RIGHTVALUE as RIGHTVALUE4_0_,
        section0_.SECTIONLAYOUT_ID as SECTIONL8_4_0_,
        section0_.UNIQUENAME as UNIQUENAME4_0_
    from
        SECTION section0_
    where
        section0_.SECTION_ID=?


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.