2012-04-01 54 views
0

我有一個有趣的問題,休眠,模型如下所示:休眠甚至生成多個SQL查詢,當我使用LEFT JOIN取

@NamedQueries({ 
    @NamedQuery(name = "A.test", query = "SELECT DISTINCT a FROM A a LEFT JOIN FETCH a.pk.b WHERE a.pk.b.pk.c.id = :cId AND a.pk.b.pk.d.id = :dId") 
}) 
@Entity 
@Table(name = "AT") 
public class A implements Serializable { 

    @EmbeddedId 
    private APK pk; 
} 

@Embeddable 
public class APK implements Serializable { 

    @ManyToOne 
    @JoinColumns({@JoinColumn(name = "***", referencedColumnName = "***"), 
        @JoinColumn(name = "***", referencedColumnName = "***") 
    }) 
    private B b; 
} 

@Entity 
@Table(name = "BT") 
public class B implements Serializable { 

    @EmbeddedId 
    private BPK pk; 
} 


@Embeddable 
public class APK implements Serializable { 

    @ManyToOne 
    @JoinColumn(name = "***", referencedColumnName = "***") 
    private C c; 

    @ManyToOne 
    @JoinColumn(name = "***", referencedColumnName = "***") 
    private D d; 
} 

的問題是,在命名查詢,使額外的SQL查詢.. 。 問題是什麼? 謝謝!

回答

1

Hibernate默認提取多對一的引用。在您的命名查詢中選擇「A」時,會有許多多對一的引用被解析,這些引用將對應於您所看到的SQL查詢。這種默認的休眠行爲可以通過在多對一的註釋上明智地放置延遲加載來覆蓋,即:

@ManyToOne(fetch=FetchType.LAZY) 
+0

謝謝!它正在工作。但我已閱讀@ManyToOne關聯中的fetchtype設置對查詢沒有任何影響。我想我錯過了一些東西,所以我回到手冊。 :S – nerd 2012-04-01 13:24:33