2011-08-01 21 views
0

我最近用一個內存H2數據庫替換了PostgreSql單元測試數據庫。我無法弄清楚爲什麼幾個測試失敗,它在Postgre中工作正常。有約。這個應用程序中有280多個單元測試。失敗的dao測試正在從視圖中進行選擇,並且實體具有EmbeddedId,即那個視圖的列。 請參見下面的代碼(注:我改了個名字,代碼和隱藏寫這封郵件時,實名SQL,但是這是有Postgre DB工作單元測試)從H2中的視圖選擇不起作用

<pre> 

@Table(name = "item_view") // <- item_view is a database view 
public class ItemV implements Serializable 
{ 
    ..... 
    @EmbeddedId // <- entity has an embedded id 
    private ItemVId id; 
    ..... 

@Embeddable 
    public static class ItemVId implements Serializable //<- This is the embeddedId 
    { 
     @Column(name = "item_id", updatable=false, insertable=false) 
     private Long ItemId; //<- col no.1 of view 

     @Column(name = "item_type_id", updatable=false, insertable=false) 
     private Integer ItemTypeId; //<- col no.2 of view 
    .....  
ItemType is an enum 

And the view is 
CREATE OR REPLACE VIEW item_view AS 
     (  (  (  SELECT pt.id as item_id, cit.id as item_type_id 
            FROM xyz pt, item_type cit 
            WHERE pt.name::text = 'xyz'::text 
         UNION 
           SELECT z.id as item_id, cit.id as item_type_id 
            FROM zzz z, item_type cit 
            WHERE z.name::text = 'zzz'::text) 
       .............. 

and the dao method is 

    public ItemView find(Long itemId, ItemType itemType) 
    { 
     String hql = " from ItemV iv where iv.id.itemId = :itemId and iv.id.itemTypeId = :itemTypeId "); 
     List<ItemView> result = (List<ItemView>)getEntityManager() 
      .createQuery(hql) 
      .setParameter("itemId", itemId) 
      .setParameter("itemTypeId", itemType.getId()) 
      .setMaxResults(1) 
      .getResultList(); 
     return result.isEmpty() 
      ? null : result.get(0); 
    } 

這道法總是返回空結果,從不在視圖中查找現有行?我知道這些行存在,因爲當我在同一個dao上得到All()時,我看到了結果,並且我看到了匹配條件的行。

從H2數據庫的視圖中選擇行有什麼特別之處嗎? 謝謝

+0

如果您使用持久H2數據庫會發生什麼?關閉最後一次連接時,內存數據庫的內容會丟失(默認情況下)。 –

+0

我沒有嘗試,我使用「DB_CLOSE_DELAY = -1」來確保數據庫沒有關閉。除此之外,我創建了一個服務器/網絡服務器,因此當我在測試過程中放置​​一個斷點時,我可以看到該視圖存在,它不會讓我看到視圖中有什麼(我將在後面發佈錯誤消息)。我會嘗試使用持久數據庫並讓你知道。 – aug70co

+0

當我連接到內部Web服務器我得到「超時嘗試鎖定表」,並且該表是視圖的基礎表之一 – aug70co

回答

1

確定固定,我不得不使用一個較小的數字爲LOCK_TIMEOUT值,所以現在我可以連接到數據庫並查看值。從視圖中選擇問題也是固定的。我不得不說H2是非常整潔優雅的。我很高興,我將單元測試分貝轉換爲H2。