1 16 17 package org.springframework.orm.hibernate3; 18 19 import java.sql.SQLException ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.hibernate.FlushMode; 24 import org.hibernate.HibernateException; 25 import org.hibernate.Interceptor; 26 import org.hibernate.JDBCException; 27 import org.hibernate.Session; 28 import org.hibernate.SessionFactory; 29 import org.hibernate.exception.GenericJDBCException; 30 31 import org.springframework.beans.BeansException; 32 import org.springframework.beans.factory.BeanFactory; 33 import org.springframework.beans.factory.BeanFactoryAware; 34 import org.springframework.beans.factory.InitializingBean; 35 import org.springframework.core.Constants; 36 import org.springframework.dao.DataAccessException; 37 import org.springframework.jdbc.support.SQLExceptionTranslator; 38 39 52 public abstract class HibernateAccessor implements InitializingBean, BeanFactoryAware { 53 54 63 public static final int FLUSH_NEVER = 0; 64 65 76 public static final int FLUSH_AUTO = 1; 77 78 95 public static final int FLUSH_EAGER = 2; 96 97 107 public static final int FLUSH_COMMIT = 3; 108 109 117 public static final int FLUSH_ALWAYS = 4; 118 119 120 121 private static final Constants constants = new Constants(HibernateAccessor.class); 122 123 124 protected final Log logger = LogFactory.getLog(getClass()); 125 126 private SessionFactory sessionFactory; 127 128 private Object entityInterceptor; 129 130 private SQLExceptionTranslator jdbcExceptionTranslator; 131 132 private SQLExceptionTranslator defaultJdbcExceptionTranslator; 133 134 private int flushMode = FLUSH_AUTO; 135 136 private String [] filterNames; 137 138 142 private BeanFactory beanFactory; 143 144 145 149 public void setSessionFactory(SessionFactory sessionFactory) { 150 this.sessionFactory = sessionFactory; 151 } 152 153 157 public SessionFactory getSessionFactory() { 158 return this.sessionFactory; 159 } 160 161 175 public void setEntityInterceptorBeanName(String entityInterceptorBeanName) { 176 this.entityInterceptor = entityInterceptorBeanName; 177 } 178 179 192 public void setEntityInterceptor(Interceptor entityInterceptor) { 193 this.entityInterceptor = entityInterceptor; 194 } 195 196 206 public Interceptor getEntityInterceptor() throws IllegalStateException , BeansException { 207 if (this.entityInterceptor instanceof String ) { 208 if (this.beanFactory == null) { 209 throw new IllegalStateException ("Cannot get entity interceptor via bean name if no bean factory set"); 210 } 211 return (Interceptor) this.beanFactory.getBean((String ) this.entityInterceptor, Interceptor.class); 212 } 213 return (Interceptor) this.entityInterceptor; 214 } 215 216 227 public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { 228 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 229 } 230 231 234 public SQLExceptionTranslator getJdbcExceptionTranslator() { 235 return this.jdbcExceptionTranslator; 236 } 237 238 245 public void setFlushModeName(String constantName) { 246 setFlushMode(constants.asNumber(constantName).intValue()); 247 } 248 249 255 public void setFlushMode(int flushMode) { 256 this.flushMode = flushMode; 257 } 258 259 262 public int getFlushMode() { 263 return this.flushMode; 264 } 265 266 277 public void setFilterName(String filter) { 278 this.filterNames = new String [] {filter}; 279 } 280 281 292 public void setFilterNames(String [] filterNames) { 293 this.filterNames = filterNames; 294 } 295 296 299 public String [] getFilterNames() { 300 return this.filterNames; 301 } 302 303 308 public void setBeanFactory(BeanFactory beanFactory) { 309 this.beanFactory = beanFactory; 310 } 311 312 public void afterPropertiesSet() { 313 if (getSessionFactory() == null) { 314 throw new IllegalArgumentException ("Property 'sessionFactory' is required"); 315 } 316 } 317 318 319 329 protected FlushMode applyFlushMode(Session session, boolean existingTransaction) { 330 if (getFlushMode() == FLUSH_NEVER) { 331 if (existingTransaction) { 332 FlushMode previousFlushMode = session.getFlushMode(); 333 if (!previousFlushMode.lessThan(FlushMode.COMMIT)) { 334 session.setFlushMode(FlushMode.NEVER); 335 return previousFlushMode; 336 } 337 } 338 else { 339 session.setFlushMode(FlushMode.NEVER); 340 } 341 } 342 else if (getFlushMode() == FLUSH_EAGER) { 343 if (existingTransaction) { 344 FlushMode previousFlushMode = session.getFlushMode(); 345 if (!previousFlushMode.equals(FlushMode.AUTO)) { 346 session.setFlushMode(FlushMode.AUTO); 347 return previousFlushMode; 348 } 349 } 350 else { 351 } 353 } 354 else if (getFlushMode() == FLUSH_COMMIT) { 355 if (existingTransaction) { 356 FlushMode previousFlushMode = session.getFlushMode(); 357 if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) { 358 session.setFlushMode(FlushMode.COMMIT); 359 return previousFlushMode; 360 } 361 } 362 else { 363 session.setFlushMode(FlushMode.COMMIT); 364 } 365 } 366 else if (getFlushMode() == FLUSH_ALWAYS) { 367 if (existingTransaction) { 368 FlushMode previousFlushMode = session.getFlushMode(); 369 if (!previousFlushMode.equals(FlushMode.ALWAYS)) { 370 session.setFlushMode(FlushMode.ALWAYS); 371 return previousFlushMode; 372 } 373 } 374 else { 375 session.setFlushMode(FlushMode.ALWAYS); 376 } 377 } 378 return null; 379 } 380 381 387 protected void flushIfNecessary(Session session, boolean existingTransaction) throws HibernateException { 388 if (getFlushMode() == FLUSH_EAGER || (!existingTransaction && getFlushMode() != FLUSH_NEVER)) { 389 logger.debug("Eagerly flushing Hibernate session"); 390 session.flush(); 391 } 392 } 393 394 395 405 public DataAccessException convertHibernateAccessException(HibernateException ex) { 406 if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) { 407 return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator()); 408 } 409 else if (GenericJDBCException.class.equals(ex.getClass())) { 410 return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator()); 411 } 412 return SessionFactoryUtils.convertHibernateAccessException(ex); 413 } 414 415 423 protected DataAccessException convertJdbcAccessException(JDBCException ex, SQLExceptionTranslator translator) { 424 return translator.translate("Hibernate operation: " + ex.getMessage(), ex.getSQL(), ex.getSQLException()); 425 } 426 427 437 protected DataAccessException convertJdbcAccessException(SQLException ex) { 438 SQLExceptionTranslator translator = getJdbcExceptionTranslator(); 439 if (translator == null) { 440 translator = getDefaultJdbcExceptionTranslator(); 441 } 442 return translator.translate("Hibernate-related JDBC operation", null, ex); 443 } 444 445 451 protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() { 452 if (this.defaultJdbcExceptionTranslator == null) { 453 this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory()); 454 } 455 return this.defaultJdbcExceptionTranslator; 456 } 457 458 459 465 protected void enableFilters(Session session) { 466 String [] filterNames = getFilterNames(); 467 if (filterNames != null) { 468 for (int i = 0; i < filterNames.length; i++) { 469 session.enableFilter(filterNames[i]); 470 } 471 } 472 } 473 474 480 protected void disableFilters(Session session) { 481 String [] filterNames = getFilterNames(); 482 if (filterNames != null) { 483 for (int i = 0; i < filterNames.length; i++) { 484 session.disableFilter(filterNames[i]); 485 } 486 } 487 } 488 489 } 490 | Popular Tags |