Hibernate学习笔记
来源:互联网 发布:sql查询去掉重复记录 编辑:程序博客网 时间:2024/06/06 02:50
SessionFactory对象是线程安全的,但是Session对象不是。
SessionFactory对象是线程安全的,但是Session对象不是。
Hibernate的dirty checking跟领域对象的equals/hashCode没有关系(Hibernate采用自己的方式来检查实体对象的状态是否改变)。
从数据库加载的实体对象的主键不能修改,否则将抛出异常:identifier of an instance of (...) was altered from X to Y。
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个
新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全
依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,
因为这样获得的Session并没有被事务管理。
Value objects 的生命周期完全取决于其所属的Entity objects,并且value bojects不能在entity object之间共享。
一种java类型可以映射到多个Hibernate类型,此时我们需要明确指出类型映射。
Hibernate的element标签用于映射value object而不是entity object。
bag是和set相似,是无序的,但是允许重复的元素出现。
由于component映射的类没有id,故需要实现equals和hashcode方法已区别不同的component类。
对于Value object来说,通过 <component>进行映射,如果需要映射一个value object的集合,则可以使用<element> 或 <composite-element>进行映射。
delete-orphan 只作用于one-to-many的集合映射。
异常:object references an unsaved transient instance - save the transient instance before flushing : 将cascade属性改为save-update或all。
异常:failed to lazily initialize a collection of role: davenkin.opinions.domain.Survey.options, could not initialize proxy - no Session, 将lazy设置成"false"。
在配置fetch时,如果是“select”, Hibernate将使用lazy loading(如果开启),此时association将会在实际访问是通过select语句加载;而如果使用的是fetch=join,Hibernate将忽略lazy loading,直接一次性通过out join将所有的association加载返回。
Hibernate的dirty checking跟领域对象的equals/hashCode没有关系(Hibernate采用自己的方式来检查实体对象的状态是否改变)。
从数据库加载的实体对象的主键不能修改,否则将抛出异常:identifier of an instance of (...) was altered from X to Y。
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个
新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全
依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,
因为这样获得的Session并没有被事务管理。
SessionFactory对象是线程安全的,但是Session对象不是。
Hibernate的dirty checking跟领域对象的equals/hashCode没有关系(Hibernate采用自己的方式来检查实体对象的状态是否改变)。
从数据库加载的实体对象的主键不能修改,否则将抛出异常:identifier of an instance of (...) was altered from X to Y。
SessionFactory的getCurrentSession并不能保证在没有当前Session的情况下会自动创建一个
新的,这取决于CurrentSessionContext的实现,SessionFactory将调用CurrentSessionContext的currentSession()方法来获得Session。在Spring中,如果我们在没有配置TransactionManager并且没有事先调用SessionFactory.openSession()的情况直接调用getCurrentSession(),那么程序将抛出“No Session found for current thread”异常。如果配置了TranactionManager并且通过@Transactional或者声明的方式配置的事务边界,那么Spring会在开始事务之前通过AOP的方式为当前线程创建Session,此时调用getCurrentSession()将得到正确结果。
然而,产生以上异常的原因在于Spring提供了自己的CurrentSessionContext实现,如果我们不打算使用Spring,而是自己直接从hibernate.cfg.xml创建SessionFactory,并且为在hibernate.cfg.xml
中设置current_session_context_class为thread,也即使用了ThreadLocalSessionContext,那么我们在调用getCurrentSession()时,如果当前线程没有Session存在,则会创建一个绑定到当前线程。
Hibernate在默认情况下会使用JTASessionContext,Spring提供了自己SpringSessionContext,因此我们不用配置current_session_context_class,当Hibernate与Spring集成时,将使用该SessionContext,故此时调用getCurrentSession()的效果完全
依赖于SpringSessionContext的实现。
在没有Spring的情况下使用Hibernate,如果没有在hibernate.cfg.xml中配置current_session_context_class,有没有JTA的话,那么程序将抛出"No CurrentSessionContext configured!"异常。此时的解决办法是在hibernate.cfg.xml中将current_session_context_class配置成thread。
在Spring中使用Hibernate,如果我们配置了TransactionManager,那么我们就不应该调用SessionFactory的openSession()来获得Sessioin,
因为这样获得的Session并没有被事务管理。
Value objects 的生命周期完全取决于其所属的Entity objects,并且value bojects不能在entity object之间共享。
一种java类型可以映射到多个Hibernate类型,此时我们需要明确指出类型映射。
Hibernate的element标签用于映射value object而不是entity object。
bag是和set相似,是无序的,但是允许重复的元素出现。
由于component映射的类没有id,故需要实现equals和hashcode方法已区别不同的component类。
对于Value object来说,通过 <component>进行映射,如果需要映射一个value object的集合,则可以使用<element> 或 <composite-element>进行映射。
delete-orphan 只作用于one-to-many的集合映射。
异常:object references an unsaved transient instance - save the transient instance before flushing : 将cascade属性改为save-update或all。
异常:failed to lazily initialize a collection of role: davenkin.opinions.domain.Survey.options, could not initialize proxy - no Session, 将lazy设置成"false"。
在配置fetch时,如果是“select”, Hibernate将使用lazy loading(如果开启),此时association将会在实际访问是通过select语句加载;而如果使用的是fetch=join,Hibernate将忽略lazy loading,直接一次性通过out join将所有的association加载返回。
Value objects 的生命周期完全取决于其所属的Entity objects,并且value bojects不能在entity object之间共享。
一种java类型可以映射到多个Hibernate类型,此时我们需要明确指出类型映射。
Hibernate的element标签用于映射value object而不是entity object。
bag是和set相似,是无序的,但是允许重复的元素出现。
由于component映射的类没有id,故需要实现equals和hashcode方法已区别不同的component类。
对于Value object来说,通过 <component>进行映射,如果需要映射一个value object的集合,则可以使用<element> 或 <composite-element>进行映射。
delete-orphan 只作用于one-to-many的集合映射。
异常:object references an unsaved transient instance - save the transient instance before flushing : 将cascade属性改为save-update或all。
异常:failed to lazily initialize a collection of role: davenkin.opinions.domain.Survey.options, could not initialize proxy - no Session, 将lazy设置成"false"。
在配置fetch时,如果是“select”, Hibernate将使用lazy loading(如果开启),此时association将会在实际访问是通过select语句加载;而如果使用的是fetch=join,Hibernate将忽略lazy loading,直接一次性通过out join将所有的association加载返回。
- 【Hibernate学习笔记】hibernate入门
- Hibernate学习笔记:初识Hibernate
- Hibernate Synchronizer 学习笔记
- hibernate学习笔记1
- Hibernate 学习笔记
- Hibernate学习笔记
- Hibernate 学习笔记
- Hibernate 实例学习笔记
- Hibernate学习笔记(1)
- hibernate学习笔记
- Hibernate学习笔记(一)
- Hibernate学习笔记
- Hibernate学习笔记!- -
- Hibernate学习笔记(一)
- Hibernate学习笔记
- Hibernate 学习笔记
- Hibernate Synchronizer 学习笔记
- Hibernate 学习笔记
- sprite kit 精灵运动
- pyhon BeautifulSoup模块的安装
- Objective-C 继承
- centos 安装与操作
- linux命令补全设置
- Hibernate学习笔记
- Why are EPS files generated by gnuplot rotated 90 degrees?
- Top 10 Methods for Java Arrays
- 阿里2014年校园题目最后一题答案及证明
- 分数的求和--char3
- 大小端转换
- CentOS5.5 下搭建 PHP 环境(最佳的LAMP环境)
- 激光夜视系统介绍
- HTML那点事之【超链接】