JiveJdon Community Forums
在线322人 J道首页 | 论坛首页 | 培训咨询 | 开源框架 | 精华 | 查搜 | 注册 | 登陆 |
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表
???en_US.forumThreadNext.name??? 下一主题
这个主题共有 1 回复 / 1 页 [ ]  发表新帖子  回复该主题贴
lyojbuilder

发表文章: 196
注册时间: 2003年03月14日 00:34
如何迁移 Hibernate架构到 spring架构? 发表: 2007年05月03日 22:57 回复
以前我的项目使用Hibernate+struts架构的,所有的Dao类后台使用的操作数据库都用Hibernate,统一使用一个类得到session(就是Hibernate实例的工具类,使用静态变量sessionfactory),现在有没有方法给项目增加Spring的支持,使这个工具类中的Session交给Spring管理起来? 就是说这个sessionFactory不是静态变量,是由Spring注入进来的,所有的Session都是由Spring创建的sessionFactory来得到?我得HibernateUtil这个工具类内容如下:


代码
public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static final SessionFactory sessionFactory;

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

static {
try {
//URL configFileURL = null;
URL configFileURL = HibernateUtil.class.getResource(_configFilePath);
log.info(
"lyo get config URL: "+configFileURL);
// Create the SessionFactory from Hibernate.cfg.xml
sessionFactory = new Configuration().configure(configFileURL).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println(
"Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}


public static Session getHibernateCurrentSession(){
return getSessionFactory().getCurrentSession();

}
public static void closeSession(){
this.getSessionFactory().getCurrentSession().close();
}

}


如果用 Spring 来管理起来这个类呢? 我试过用 Spring把 sessionFactory注入这个类中,但是总是提示说不能在无事务环境下,使用 session? 有没有做过这种情况的? 请指点!
banq

发表文章: 8929
注册时间: 2002年08月03日 17:08
回复:如何迁移 Hibernate架构到 spring架构? 发表: 2007年05月08日 11:28 回复
spring中有一个专门的Hibernate类,就不需要Hibernate.cfg这样全局配置,转到Spring的配置中配置,参考Spring源码中案例appfuse
这个主题有 1 回复 / 1 页 [ ]
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表    返回页首  返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Spring Struts
查询本论坛内 回复超过的热门帖子
快速发表回复
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-07 jdon.com

anti spam