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]