hibernate+spring中怎么得到connection

hwt_211 2012-05-11 03:08:17
整合hibernate 和 spring的一个项目,其中怎么得到一个connection对象,我想使用connection对象操作存储过程

发现session.connection() 这个方法已经过时了。

怎么才能得到connection对象?
...全文
534 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MR_ZHOUXING 2013-04-09
  • 打赏
  • 举报
回复
关键是怎么操作啊,求指导,非常感谢啊
new_bird_0001 2012-05-11
  • 打赏
  • 举报
回复
如果spring中配置有datasource,可直接获取spring中的datasource来获得connection,不一定通过hibernate来获取。
hwt_211 2012-05-11
  • 打赏
  • 举报
回复
1,只要先得到sessionFactory对象
2,然后通过sessionFactory的子类SessionFactoryImplementor 的 getConnectionProvider() 来 得到ConnectionProvider
3,connectionProvider。getConnection();

ConnectionProvider cp = ((SessionFactoryImplementor)factory).getConnectionProvider();
Connection connection = cp.getConnection();
得到了connection对象,下面就是jdbc代码了
记得关闭资源
dqsweet 2012-05-11
  • 打赏
  • 举报
回复

public class HibernateSessionFactory {



/**

* Location of hibernate.cfg.xml file.

* Location should be on the classpath as Hibernate uses

* #resourceAsStream style lookup for its configuration file.

* The default classpath location of the hibernate config file is

* in the default package. Use #setConfigFile() to update

* the location of the configuration file for the current session.

*/

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

private static Configuration configuration = new Configuration();

private static org.hibernate.SessionFactory sessionFactory;

private static String configFile = CONFIG_FILE_LOCATION;



static {

try {

configuration.configure(configFile);

sessionFactory = configuration.buildSessionFactory();

} catch (Exception e) {

System.err

.println("%%%% Error Creating SessionFactory %%%%");

e.printStackTrace();

}

}

private HibernateSessionFactory() {

}

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧