1

由于在hibernate 3中不推荐使用buildSessionFactory方法,我们必须通过ServiceRegistry创建会话工厂。我已经创建了它,如下所示,

 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() 中的演员,对吗?

为什么我不能在那里使用泛型,例如,

(Map<String,String>)configuration.getProperties()

上面也是初始化服务注册表的正确方法,因为 applySettings() 方法将 Map 作为参数?

4

1 回答 1

0

可以使用

(Map<String,String>)configuration.getProperties()

但它会生成一个警告,因为在执行转换时(在运行时),无法检查返回的 Map 是否具有 String 作为类型参数,因为该信息不再可用。实际上,编译后,演员表变成了

(Map)configuration.getProperties()

看起来虽然地图在实践中可能只包含字符串,但您不应该依赖它,而应该只使用Map<?,?>

于 2012-12-19T16:14:56.630 回答