gpt4 book ai didi

java - 在hibernate 4中初始化ServiceRegistry

转载 作者:行者123 更新时间:2023-12-01 04:56:59 26 4
gpt4 key购买 nike

由于 buildSessionFactory 方法在 hibernate 3 中已被弃用,因此我们必须通过 ServiceRegistry 创建 session 工厂。我已经像下面一样创建了它,

 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 作为参数?

最佳答案

可以使用

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

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

(Map)configuration.getProperties()

虽然 map 实际上可能只包含字符串,但您不应该依赖于此,而应该使用 Map<?,?>

关于java - 在hibernate 4中初始化ServiceRegistry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13948643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com