2012-12-19 106 views
1

由於buildSessionFactory方法在休眠3已經過時,我們必須通過ServiceRegistry創建會話工廠。我已經創造了它下面喜歡,初始化ServiceRegistry休眠4

Configuration configuration = new Configuration().configure(); 
Map<String, String> map = new HashMap<String, String>((Map)configuration 
     .getProperties()); 
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(map) 
     .buildServiceRegistry(); 

但它顯示我像下面的PMD錯誤,

Multiple markers at this line 
    - Type safety: The expression of type Map needs unchecked conversion to conform to Map<? extends String,? extends 
    String> 
    - Map is a raw type. References to generic type Map<K,V> should be parameterized 

我應該如何避免呢?這是因爲在(Map)configuration.getProperties()中投了對嗎?

爲什麼不能我使用泛型那裏像,

​​3210

而且上面是初始化服務註冊表的正確方法正確的,因爲()方法接受一個Map作爲參數applySettings?

回答

0

可以使用

​​3210

,但因爲在執行CAST(在運行時)時生成一個警告,不能進行檢查,返回的地圖具有String作爲類型參數,因爲該信息不再可用。實際上,編譯後,中投成爲

(Map)configuration.getProperties() 

好像雖然地圖可能包含在實踐中,只有字符串,你不應該依賴於這一點,應該只使用Map<?,?>

+0

的(地圖)給我編譯錯誤,無法從屬性轉換爲Map 和Map 給我編譯錯誤構造函數HashMap (Map )未定義 – FrankD