汤阳光Hibernate3.6(完整版)(11)
时间:2025-07-10
时间:2025-07-10
1, 属性文件(hibernate.properties)
Configuration cfg = new Configuration();
2, Xml文件(hibernate.cfg.xml)
a) 加载默认名称的配置文件(hibernate.cfg.xml)
Configuration cfg = new Configuration().configure(); b) 或加载指定名称的配置文件:
Configuration cfg = new Configuration()
.configure(―myhibernate.cfg.xml‖);
3.2. SessionFactory
Configuration对象根据当前的配置信息生成 SessionFactory 对象。SessionFactory 对象一旦构造完毕,即被赋予特定的配置信息(SessionFactory 对象中保存了当前的数据库配置信息和所有映射关系以及预定义的SQL语句。同时,SessionFactory还负责维护Hibernate的二级缓存)。 相关代码如下: Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory();
2, SessionFactory是线程安全的。
3, SessionFactory是生成Session的工厂:
Session session = sessionFactory.openSession();
4, 构造 SessionFactory 很消耗资源,一般情况下一个应用中只初始化一个
SessionFactory 对象。
3.3. Session
Session 是应用程序与数据库之间交互操作的一个单线程对象,是 Hibernate 运作的中心,所有持久化对象必须在 session 的管理下才可以进行持久化操作。此对象的生命周期很短。Session 中有一个缓存,显式执行flush()方法之前,所有的持久层操作的数据都缓存在 session 对象处。(相当于 JDBC 中的 Connection)
上一篇:公众安全感调查问卷
下一篇:旋转机械振动诊断标准研究