java Hibernate 4.1至5.1 SessionFactory ConnectionProvider
如何从hibernate 5中的会话工厂获得连接提供程序?获取连接的方法不再存在,也不会被javadocs中的任何内容所取代。此代码段在4.1中有效,但在5.1中不起作用(具体来说,getConnectionProvider()不存在)
private SessionFactory factory;
private ServletOutputStream outputStream;
private ServletContext context;
public Object execute(Map properties) {
InputStream input = null;
try {
Session session = factory.getCurrentSession();
SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider connectionProvider = sessionFactoryImplementation).getConnectionProvider();
Connection conn = connectionProvider.getConnection();
# 1 楼答案
对于hibernate 5.2.10,请尝试以下操作:
见:
Hibernate - Deprecated API
Docs Hibernate 4.1 - SessionFactoryImplementor.getConnectionProvider()
# 2 楼答案
正如Allinger Medeiros所说,ConnectionProvider在Hibernate 4中被弃用,在Hibernate 5中不可用。 然而,正如Gwaptiva指出的,他的解决方案导致了ClassCastException
这对我很有用: