1 16 17 package org.springframework.orm.hibernate; 18 19 import java.sql.SQLException ; 20 21 import net.sf.hibernate.FlushMode; 22 import net.sf.hibernate.HibernateException; 23 import net.sf.hibernate.Interceptor; 24 import net.sf.hibernate.JDBCException; 25 import net.sf.hibernate.Session; 26 import net.sf.hibernate.SessionFactory; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.springframework.beans.BeansException; 31 import org.springframework.beans.factory.BeanFactory; 32 import org.springframework.beans.factory.BeanFactoryAware; 33 import org.springframework.beans.factory.InitializingBean; 34 import org.springframework.core.Constants; 35 import org.springframework.dao.DataAccessException; 36 import org.springframework.jdbc.support.SQLExceptionTranslator; 37 38 51 public abstract class HibernateAccessor implements InitializingBean, BeanFactoryAware { 52 53 62 public static final int FLUSH_NEVER = 0; 63 64 75 public static final int FLUSH_AUTO = 1; 76 77 94 public static final int FLUSH_EAGER = 2; 95 96 106 public static final int FLUSH_COMMIT = 3; 107 108 109 110 private static final Constants constants = new Constants(HibernateAccessor.class); 111 112 113 protected final Log logger = LogFactory.getLog(getClass()); 114 115 private SessionFactory sessionFactory; 116 117 private Object entityInterceptor; 118 119 private SQLExceptionTranslator jdbcExceptionTranslator; 120 121 private int flushMode = FLUSH_AUTO; 122 123 127 private BeanFactory beanFactory; 128 129 130 134 public void setSessionFactory(SessionFactory sessionFactory) { 135 this.sessionFactory = sessionFactory; 136 } 137 138 142 public SessionFactory getSessionFactory() { 143 return this.sessionFactory; 144 } 145 146 160 public void setEntityInterceptorBeanName(String entityInterceptorBeanName) { 161 this.entityInterceptor = entityInterceptorBeanName; 162 } 163 164 177 public void setEntityInterceptor(Interceptor entityInterceptor) { 178 this.entityInterceptor = entityInterceptor; 179 } 180 181 191 public Interceptor getEntityInterceptor() throws IllegalStateException , BeansException { 192 if (this.entityInterceptor instanceof String ) { 193 if (this.beanFactory == null) { 194 throw new IllegalStateException ("Cannot get entity interceptor via bean name if no bean factory set"); 195 } 196 return (Interceptor) this.beanFactory.getBean((String ) this.entityInterceptor, Interceptor.class); 197 } 198 return (Interceptor) this.entityInterceptor; 199 } 200 201 214 public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { 215 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 216 } 217 218 223 public synchronized SQLExceptionTranslator getJdbcExceptionTranslator() { 224 if (this.jdbcExceptionTranslator == null) { 225 this.jdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory()); 226 } 227 return this.jdbcExceptionTranslator; 228 } 229 230 237 public void setFlushModeName(String constantName) { 238 setFlushMode(constants.asNumber(constantName).intValue()); 239 } 240 241 247 public void setFlushMode(int flushMode) { 248 this.flushMode = flushMode; 249 } 250 251 254 public int getFlushMode() { 255 return this.flushMode; 256 } 257 258 263 public void setBeanFactory(BeanFactory beanFactory) { 264 this.beanFactory = beanFactory; 265 } 266 267 public void afterPropertiesSet() { 268 if (getSessionFactory() == null) { 269 throw new IllegalArgumentException ("Property 'sessionFactory' is required"); 270 } 271 } 272 273 274 284 protected FlushMode applyFlushMode(Session session, boolean existingTransaction) { 285 if (getFlushMode() == FLUSH_NEVER) { 286 if (existingTransaction) { 287 FlushMode previousFlushMode = session.getFlushMode(); 288 if (!previousFlushMode.equals(FlushMode.NEVER)) { 289 session.setFlushMode(FlushMode.NEVER); 290 return previousFlushMode; 291 } 292 } 293 else { 294 session.setFlushMode(FlushMode.NEVER); 295 } 296 } 297 else if (getFlushMode() == FLUSH_EAGER) { 298 if (existingTransaction) { 299 FlushMode previousFlushMode = session.getFlushMode(); 300 if (!previousFlushMode.equals(FlushMode.AUTO)) { 301 session.setFlushMode(FlushMode.AUTO); 302 return previousFlushMode; 303 } 304 } 305 else { 306 } 308 } 309 else if (getFlushMode() == FLUSH_COMMIT) { 310 if (existingTransaction) { 311 FlushMode previousFlushMode = session.getFlushMode(); 312 if (previousFlushMode.equals(FlushMode.AUTO)) { 313 session.setFlushMode(FlushMode.COMMIT); 314 return previousFlushMode; 315 } 316 } 317 else { 318 session.setFlushMode(FlushMode.COMMIT); 319 } 320 } 321 return null; 322 } 323 324 330 protected void flushIfNecessary(Session session, boolean existingTransaction) throws HibernateException { 331 if (getFlushMode() == FLUSH_EAGER || (!existingTransaction && getFlushMode() != FLUSH_NEVER)) { 332 logger.debug("Eagerly flushing Hibernate session"); 333 session.flush(); 334 } 335 } 336 337 338 349 public DataAccessException convertHibernateAccessException(HibernateException ex) { 350 if (ex instanceof JDBCException) { 351 return convertJdbcAccessException((JDBCException) ex); 352 } 353 return SessionFactoryUtils.convertHibernateAccessException(ex); 354 } 355 356 363 protected DataAccessException convertJdbcAccessException(JDBCException ex) { 364 return getJdbcExceptionTranslator().translate( 365 "Hibernate operation: " + ex.getMessage(), null, ex.getSQLException()); 366 } 367 368 378 protected DataAccessException convertJdbcAccessException(SQLException ex) { 379 return getJdbcExceptionTranslator().translate("Hibernate operation", null, ex); 380 } 381 382 } 383 | Popular Tags |