zzeng

zzeng

为什么 XX.hbm.xml 和 XX.cfg.xml这两个文件要放在/WEB-INF/classes/这个目录下呢?

为什么 XX.hbm.xml 和 XX.cfg.xml这两个文件要放在/WEB-INF/classes/这个目录下呢?

因为你的所 有类放在/web-inf/classes/目录下,所以new Configuration().configure方法是以当前目录为根目录(也就/web-inf/classes/目录下)来寻找 xx.cfg.xml,否则hibernate是找不到的,除非你使用绝对路径,或url(全球资源定位器);

SessionFactory sf=new Configuration().configure("/hibernate/hibernate.cfg.xml").buildSessionFactory();
hibernate会去/web-inf/classes/hibernate/目录下去找到这个hibernate.cfg.xml

SessionFactory sf=new Configuration().configure().buildSessionFactory();
hibernate会去/web-inf/classes/目录下去找到这个hibernate.cfg.xml

当 你想用Session类的save方法把VO变成PO的时候,你就会想到Session是怎么来的,Session的对象是通过 SessionFactory类的openSession方法来实例化。有了SessionFactory你又会想到SessionFactory是怎么 来的,SessionFactory的对象是通过Configuration实例的来构建。

Configuration类是用来管理hibernate的配置信息,如数据库url,用户名,密码。。。
Configuration 类的buildSessionFactory()用来实例化SessionFactory 。要使用buildSessionFactory方法,必须先让Configuration.configure()方法找到 hibernate.cfg.xml文件并初始化。

SessionFactory类是用于创建Session的实例。

Session是Hibernate持久化操作和管理的核心。

posted on 2005-09-28 16:22 zzeng 阅读(1147) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: