/**
* 得到session
* @return
*/
public static Session getSession(){
// get session
Session session = getSessionFactory().openSession();
return session;
}
public static Connection getConnection(){
// first ,hibernate 4 will give up the mothod
Connection con = null;
// con = getSession().connection();
// second
SessionFactoryImplementor 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.5
SessionFactory sf = cf.buildSessionFactory();
return sf;
}
hibernate获取connection
最新推荐文章于 2021-10-14 21:58:56 发布