How to get DataSource or Connection from JPA2 EntityManager in Java EE 6

前端 未结 3 1376
傲寒
傲寒 2020-12-17 17:42

I have a working application where I use Java EE 6 with EclipseLink for persistence and a PostgreSQL database.

For the User-Registration I want to set the password i

3条回答
  •  太阳男子
    2020-12-17 18:23

    Here is a snippet of code that works with Hibernate 4, based on dulon's answer

    Connection getConnection() {
            Session session = entityManager.unwrap(Session.class);
            MyWork myWork = new MyWork();
            session.doWork(myWork);
            return myWork.getConnection();
    }
    
    
    private static class MyWork implements Work {
    
        Connection conn;
    
        @Override
        public void execute(Connection arg0) throws SQLException {
            this.conn = arg0;
        }
    
        Connection getConnection() {
            return conn;
        }
    
    }
    

提交回复
热议问题