2010-05-18 65 views
2

所以我一直在處理一個家釀Brew數據庫框架有一些嚴重的缺陷,使用的理由是不使用ORM將節省執行的查詢數量。Hierarchical Hibernate,執行多少個查詢?

如果我從可連接的對象層次結構的頂層選擇所有可能的記錄,那麼在使用ORM(如Hibernate)時將對數據庫進行多少次單獨調用?

我覺得就這一點叫廢話,因爲可聯合實體應該在一個查詢中被刪除,對嗎?我在這裏錯過了什麼嗎?

注意:由於所有記錄都將被使用,所以在這種情況下延遲初始化並不重要。

+0

可以是單個查詢。您可以快速(在10分鐘內使用Eclipse Hibernate Tools)將數據庫逆向工程化到帶有Hibernate註釋的類中,並使用您需要的查詢和SQL語句記錄(可通過Hibernate配置打開)編寫一個簡單的程序。你會知道的。 Hibernate Tools可以從http://www.hibernate.org/subprojects/tools.html下載。我建議使用它們與EE Eclipse版 – bobah 2010-05-18 18:21:15

+0

正如我想的,感謝您的快速響應哇巴:) – user344321 2010-05-18 18:27:01

+0

我不認爲這個問題可以合理地回答。自己嘗試,使用各種獲取策略,調整查詢,但沒有通用的答案。 – 2010-05-19 14:58:11

回答

0

Bobah是正確的,

你應該給爲了看看有多少請求將被然而,發送到數據庫,休眠一試,在Hibernate中你也可以指定並通過使用HQL調整的具體要求。

除了使用Hibernate工具,你也可以使用P6spy driver,這樣你就可以看到所有冬眠發送到你的數據庫,與請求的每個過濾器的值的請求。

1

Hibernate將幾乎總是使用單個查詢檢索對象層次結構;我不記得看到它做的其他事情。無論如何,這很容易測試。有了這個非常簡單的映射:

@Entity 
public static class Person { 
    @Id 
    public String name; 
} 

@Entity 
public static class Student extends Person { 
    public float averageGrade; 
} 

@Entity 
public static class Teacher extends Person { 
    public float salary; 
} 

則Hibernate給了我一個非常簡單的瀏覽查詢(sessionFactory.openSession().createCriteria(Person.class).list();)以下結果。

隨着@Inheritance(strategy = InheritanceType.SINGLE_TABLE)父:

select this_.name as name0_0_, this_.averageGrade as averageG3_0_0_, 
    this_.salary as salary0_0_, this_.DTYPE as DTYPE0_0_ from HibernateTest$Person this_ 

隨着@Inheritance(strategy = InheritanceType.JOINED)父:

select this_.name as name0_0_, this_1_.averageGrade as averageG1_1_0_, 
    this_2_.salary as salary2_0_, case when this_1_.name is not null then 1 
    when this_2_.name is not null then 2 when this_.name is not null then 0 
    end as clazz_0_ from HibernateTest$Person this_ left outer 
    join HibernateTest$Student this_1_ on this_.name=this_1_.name left outer join 
    HibernateTest$Teacher this_2_ on this_.name=this_2_.name 

隨着@Inheritance(strategy = InheritanceType.JOINED)父:

select this_.name as name0_0_, this_.averageGrade as averageG1_1_0_, 
    this_.salary as salary2_0_, this_.clazz_ as clazz_0_ from 
    (select null as averageGrade, name, null as salary, 0 as clazz_ 
    from HibernateTest$Person union select averageGrade, name, null as salary, 
    1 as clazz_ from HibernateTest$Student union select null as averageGrade, 
    name, salary, 2 as clazz_ from HibernateTest$Teacher) this_ 

正如您所看到的,每個查詢都是一個查詢,根據映射類型的不同,可以使用JOIN s或UNION s。