hibernate获取connection

来源:互联网 发布:网络宣传部是干什么的 编辑:程序博客网 时间:2024/04/20 12:22
/** * 得到session * @return */public static Session getSession(){// get sessionSession session = getSessionFactory().openSession();return session;}public static Connection getConnection(){// first ,hibernate 4 will give up the mothod Connection con = null;// con = getSession().connection();// secondSessionFactoryImplementor sfi = (SessionFactoryImplementor)getSessionFactory();try {con = sfi.getConnectionProvider().getConnection();} catch (SQLException e) {e.printStackTrace();}return con;}public static SessionFactory getSessionFactory(){Configuration cf = new Configuration().configure();// sessionFactory hibernate 4.2// SessionFactory sf = cf.buildSessionFactory();//ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cf.getProperties()).buildServiceRegistry();// SessionFactory sf = cf.buildSessionFactory(sr);// hibernate 3.5SessionFactory sf = cf.buildSessionFactory();return sf;}

0 0
原创粉丝点击