有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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(); 

共 (2) 个答案

  1. # 2 楼答案

    正如Allinger Medeiros所说,ConnectionProvider在Hibernate 4中被弃用,在Hibernate 5中不可用。 然而,正如Gwaptiva指出的,他的解决方案导致了ClassCastException

    这对我很有用:

    JdbcConnectionAccess connectionAccess = 
        ((SessionImplementor)sessionFactory.getCurrentSession())
        .getJdbcConnectionAccess();