HibernateAccessor

HibernateAccessor是HibernateInterceptor和HibernateTemplate父类,它主要提供Hibernate刷新机制的转换和Hibernate异常体系到Spring异常体系的转换

刷新机制
  Spring定义刷新选项
     FLUSH_NEVER
    FLUSH_AUTO
    FLUSH_EAGER
    FLUSH_COMMIT
    FLUSH_ALWAYS
  Hibernate定义刷新选项
   MANUAL(Flush_NEVER过时)
   COMMIT
   AUTO
   ALWAYS
  在Spring代理Hibernate操作之前,必须根据内置的刷新机制合理的改变Hibernate刷新机制,同时对于每个选项,根据Session是否绑定等情况,有不同的处理.当操作完成之后可回复。
   FLUSH_NEVER 
     已绑定:改变Hibernate FlushMode 为 MANUAL 
      未绑定:改变Hibernate FlushMode 为 MANUAL
   FLUSH_AUTO 
     已绑定:保留操作之前的Hibernate FlushMode
     未绑定:Hibernate FlushMode默认为 AUTO
   FLUSH_EAGER
     已绑定:改变Hibernate FlushMode 为 AUTO
     未绑定:Hibernate FlushMode默认为 AUTO
   FLUSH_COMMIT
     已绑定:如果之前FlushMode不为Manual,改变Hibernate FlushMode 为 COMMIT
     未绑定:改变Hibernate FlushMode 为 COMMIT
   FLUSH_ALWAYS
     已绑定:改变Hibernate FlushMode 为 ALWAYS
     未绑定:改变Hibernate FlushMode 为 ALWAYS
   在Hibernate操作成功之后,以下两种情况可刷新
      HibernateAccessor的flushMode设置为FLUSH_EAGER(HibernateInterceptor和HibernateTemplate支持)
      使用非线程绑定的Session操作并且flushMode不是FLUSH_NEVER

/**
	 * Apply the flush mode that's been specified for this accessor
	 * to the given Session.
	 * @param session the current Hibernate Session
	 * @param existingTransaction if executing within an existing transaction
	 * @return the previous flush mode to restore after the operation,
	 * or {@code null} if none
	 * @see #setFlushMode
	 * @see org.hibernate.Session#setFlushMode
	 */
	protected FlushMode applyFlushMode(Session session, boolean existingTransaction) {
		if (getFlushMode() == FLUSH_NEVER) {
			if (existingTransaction) {
				FlushMode previousFlushMode = session.getFlushMode();
				if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
					session.setFlushMode(FlushMode.MANUAL);
					return previousFlushMode;
				}
			}
			else {
				session.setFlushMode(FlushMode.MANUAL);
			}
		}
		else if (getFlushMode() == FLUSH_EAGER) {
			if (existingTransaction) {
				FlushMode previousFlushMode = session.getFlushMode();
				if (!previousFlushMode.equals(FlushMode.AUTO)) {
					session.setFlushMode(FlushMode.AUTO);
					return previousFlushMode;
				}
			}
			else {
				// rely on default FlushMode.AUTO
			}
		}
		else if (getFlushMode() == FLUSH_COMMIT) {
			if (existingTransaction) {
				FlushMode previousFlushMode = session.getFlushMode();
				if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
					session.setFlushMode(FlushMode.COMMIT);
					return previousFlushMode;
				}
			}
			else {
				session.setFlushMode(FlushMode.COMMIT);
			}
		}
		else if (getFlushMode() == FLUSH_ALWAYS) {
			if (existingTransaction) {
				FlushMode previousFlushMode = session.getFlushMode();
				if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
					session.setFlushMode(FlushMode.ALWAYS);
					return previousFlushMode;
				}
			}
			else {
				session.setFlushMode(FlushMode.ALWAYS);
			}
		}
		return null;
	}

     

你可能感兴趣的:(Hibernate)