Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wqh754186373
spring-framework
提交
0ff83606
S
spring-framework
项目概览
wqh754186373
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0ff83606
编写于
10月 11, 2011
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
committed initial Hibernate 4.0 support
上级
55f91e54
变更
18
展开全部
隐藏空白更改
内联
并排
Showing
18 changed file
with
2197 addition
and
0 deletion
+2197
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java
...ramework/orm/hibernate4/HibernateExceptionTranslator.java
+53
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java
...pringframework/orm/hibernate4/HibernateJdbcException.java
+54
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java
.../hibernate4/HibernateObjectRetrievalFailureException.java
+42
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java
...ibernate4/HibernateOptimisticLockingFailureException.java
+42
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java
...ringframework/orm/hibernate4/HibernateQueryException.java
+44
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java
...ingframework/orm/hibernate4/HibernateSystemException.java
+43
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java
...framework/orm/hibernate4/HibernateTransactionManager.java
+696
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
...ringframework/orm/hibernate4/LocalSessionFactoryBean.java
+229
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java
...g/springframework/orm/hibernate4/SessionFactoryUtils.java
+181
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java
...ava/org/springframework/orm/hibernate4/SessionHolder.java
+81
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.java
...gframework/orm/hibernate4/SpringFlushSynchronization.java
+64
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.java
...ringframework/orm/hibernate4/SpringJtaSessionContext.java
+48
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.java
.../springframework/orm/hibernate4/SpringSessionContext.java
+101
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java
...ramework/orm/hibernate4/SpringSessionSynchronization.java
+122
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/package-info.java
...java/org/springframework/orm/hibernate4/package-info.java
+17
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java
...ework/orm/hibernate4/support/OpenSessionInViewFilter.java
+180
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java
.../orm/hibernate4/support/OpenSessionInViewInterceptor.java
+176
-0
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/package-info.java
.../springframework/orm/hibernate4/support/package-info.java
+24
-0
未找到文件。
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.HibernateException
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.dao.support.PersistenceExceptionTranslator
;
/**
* {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
* instances to Spring's {@link org.springframework.dao.DataAccessException} hierarchy.
*
* @author Juergen Hoeller
* @since 3.1
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
* @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
*/
public
class
HibernateExceptionTranslator
implements
PersistenceExceptionTranslator
{
public
DataAccessException
translateExceptionIfPossible
(
RuntimeException
ex
)
{
if
(
ex
instanceof
HibernateException
)
{
return
convertHibernateAccessException
((
HibernateException
)
ex
);
}
return
null
;
}
/**
* Convert the given HibernateException to an appropriate exception from the
* {@code org.springframework.dao} hierarchy.
* @param ex HibernateException that occured
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertHibernateAccessException
*/
protected
DataAccessException
convertHibernateAccessException
(
HibernateException
ex
)
{
return
SessionFactoryUtils
.
convertHibernateAccessException
(
ex
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
java.sql.SQLException
;
import
org.hibernate.JDBCException
;
import
org.springframework.dao.UncategorizedDataAccessException
;
/**
* Hibernate-specific subclass of UncategorizedDataAccessException,
* for JDBC exceptions that Hibernate wrapped.
*
* @author Juergen Hoeller
* @since 3.1
* @see SessionFactoryUtils#convertHibernateAccessException
*/
public
class
HibernateJdbcException
extends
UncategorizedDataAccessException
{
public
HibernateJdbcException
(
JDBCException
ex
)
{
super
(
"JDBC exception on Hibernate data access: SQLException for SQL ["
+
ex
.
getSQL
()
+
"]; SQL state ["
+
ex
.
getSQLState
()
+
"]; error code ["
+
ex
.
getErrorCode
()
+
"]; "
+
ex
.
getMessage
(),
ex
);
}
/**
* Return the underlying SQLException.
*/
public
SQLException
getSQLException
()
{
return
((
JDBCException
)
getCause
()).
getSQLException
();
}
/**
* Return the SQL that led to the problem.
*/
public
String
getSql
()
{
return
((
JDBCException
)
getCause
()).
getSQL
();
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.UnresolvableObjectException
;
import
org.hibernate.WrongClassException
;
import
org.springframework.orm.ObjectRetrievalFailureException
;
/**
* Hibernate-specific subclass of ObjectRetrievalFailureException.
* Converts Hibernate's UnresolvableObjectException and WrongClassException.
*
* @author Juergen Hoeller
* @since 3.1
* @see SessionFactoryUtils#convertHibernateAccessException
*/
public
class
HibernateObjectRetrievalFailureException
extends
ObjectRetrievalFailureException
{
public
HibernateObjectRetrievalFailureException
(
UnresolvableObjectException
ex
)
{
super
(
ex
.
getEntityName
(),
ex
.
getIdentifier
(),
ex
.
getMessage
(),
ex
);
}
public
HibernateObjectRetrievalFailureException
(
WrongClassException
ex
)
{
super
(
ex
.
getEntityName
(),
ex
.
getIdentifier
(),
ex
.
getMessage
(),
ex
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.StaleObjectStateException
;
import
org.hibernate.StaleStateException
;
import
org.springframework.orm.ObjectOptimisticLockingFailureException
;
/**
* Hibernate-specific subclass of ObjectOptimisticLockingFailureException.
* Converts Hibernate's StaleObjectStateException and StaleStateException.
*
* @author Juergen Hoeller
* @since 3.1
* @see SessionFactoryUtils#convertHibernateAccessException
*/
public
class
HibernateOptimisticLockingFailureException
extends
ObjectOptimisticLockingFailureException
{
public
HibernateOptimisticLockingFailureException
(
StaleObjectStateException
ex
)
{
super
(
ex
.
getEntityName
(),
ex
.
getIdentifier
(),
ex
);
}
public
HibernateOptimisticLockingFailureException
(
StaleStateException
ex
)
{
super
(
ex
.
getMessage
(),
ex
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.QueryException
;
import
org.springframework.dao.InvalidDataAccessResourceUsageException
;
/**
* Hibernate-specific subclass of InvalidDataAccessResourceUsageException,
* thrown on invalid HQL query syntax.
*
* @author Juergen Hoeller
* @since 3.1
* @see SessionFactoryUtils#convertHibernateAccessException
*/
public
class
HibernateQueryException
extends
InvalidDataAccessResourceUsageException
{
public
HibernateQueryException
(
QueryException
ex
)
{
super
(
ex
.
getMessage
(),
ex
);
}
/**
* Return the HQL query string that was invalid.
*/
public
String
getQueryString
()
{
return
((
QueryException
)
getCause
()).
getQueryString
();
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.HibernateException
;
import
org.springframework.dao.UncategorizedDataAccessException
;
/**
* Hibernate-specific subclass of UncategorizedDataAccessException,
* for Hibernate system errors that do not match any concrete
* <code>org.springframework.dao</code> exceptions.
*
* @author Juergen Hoeller
* @since 3.1
* @see SessionFactoryUtils#convertHibernateAccessException
*/
public
class
HibernateSystemException
extends
UncategorizedDataAccessException
{
/**
* Create a new HibernateSystemException,
* wrapping an arbitrary HibernateException.
* @param cause the HibernateException thrown
*/
public
HibernateSystemException
(
HibernateException
cause
)
{
super
(
cause
!=
null
?
cause
.
getMessage
()
:
null
,
cause
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java
0 → 100644
浏览文件 @
0ff83606
此差异已折叠。
点击以展开。
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.util.Properties
;
import
javax.persistence.Embeddable
;
import
javax.persistence.Entity
;
import
javax.persistence.MappedSuperclass
;
import
javax.sql.DataSource
;
import
org.hibernate.MappingException
;
import
org.hibernate.SessionFactory
;
import
org.hibernate.cfg.Configuration
;
import
org.hibernate.cfg.Environment
;
import
org.springframework.beans.factory.DisposableBean
;
import
org.springframework.beans.factory.FactoryBean
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.context.ResourceLoaderAware
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.core.io.support.PathMatchingResourcePatternResolver
;
import
org.springframework.core.io.support.ResourcePatternResolver
;
import
org.springframework.core.io.support.ResourcePatternUtils
;
import
org.springframework.core.type.classreading.CachingMetadataReaderFactory
;
import
org.springframework.core.type.classreading.MetadataReader
;
import
org.springframework.core.type.classreading.MetadataReaderFactory
;
import
org.springframework.core.type.filter.AnnotationTypeFilter
;
import
org.springframework.core.type.filter.TypeFilter
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ReflectionUtils
;
/**
* {@link org.springframework.beans.factory.FactoryBean} that creates a
* Hibernate {@link org.hibernate.SessionFactory}. This is the usual way to
* set up a shared Hibernate SessionFactory in a Spring application context;
* the SessionFactory can then be passed to Hibernate-based DAOs via
* dependency injection.
*
* <p><b>NOTE:</b> This variant of LocalSessionFactoryBean requires Hibernate 4.0
* or higher. It is similar in role to the same-named class in the <code>orm.hibernate3</code>
* package. However, in practice, it is closer to <code>AnnotationSessionFactoryBean</code>
* since its core purpose is to bootstrap a <code>SessionFactory</code> from annotation scanning.
*
* @author Juergen Hoeller
* @since 3.1
* @see #setDataSource
* @see #setPackagesToScan
*/
public
class
LocalSessionFactoryBean
implements
FactoryBean
<
SessionFactory
>,
ResourceLoaderAware
,
InitializingBean
,
DisposableBean
{
private
static
final
String
RESOURCE_PATTERN
=
"/**/*.class"
;
private
static
final
Method
addAnnotatedClassMethod
=
ClassUtils
.
getMethodIfAvailable
(
Configuration
.
class
,
"addAnnotatedClass"
,
Class
.
class
);
private
DataSource
dataSource
;
private
Properties
hibernateProperties
;
private
String
[]
packagesToScan
;
private
Class
<?>[]
annotatedClasses
;
private
ResourcePatternResolver
resourcePatternResolver
=
new
PathMatchingResourcePatternResolver
();
private
TypeFilter
[]
entityTypeFilters
=
new
TypeFilter
[]
{
new
AnnotationTypeFilter
(
Entity
.
class
,
false
),
new
AnnotationTypeFilter
(
Embeddable
.
class
,
false
),
new
AnnotationTypeFilter
(
MappedSuperclass
.
class
,
false
)};
private
SessionFactory
sessionFactory
;
/**
* Set the DataSource to be used by the SessionFactory.
* If set, this will override corresponding settings in Hibernate properties.
* <p>If this is set, the Hibernate settings should not define
* a connection provider to avoid meaningless double configuration.
* <p>If using HibernateTransactionManager as transaction strategy, consider
* proxying your target DataSource with a LazyConnectionDataSourceProxy.
* This defers fetching of an actual JDBC Connection until the first JDBC
* Statement gets executed, even within JDBC transactions (as performed by
* HibernateTransactionManager). Such lazy fetching is particularly beneficial
* for read-only operations, in particular if the chances of resolving the
* result in the second-level cache are high.
* <p>As JTA and transactional JNDI DataSources already provide lazy enlistment
* of JDBC Connections, LazyConnectionDataSourceProxy does not add value with
* JTA (i.e. Spring's JtaTransactionManager) as transaction strategy.
* @see HibernateTransactionManager
* @see org.springframework.transaction.jta.JtaTransactionManager
* @see org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
*/
public
void
setDataSource
(
DataSource
dataSource
)
{
this
.
dataSource
=
dataSource
;
}
/**
* Set Hibernate properties, such as "hibernate.dialect".
* <p>Can be used to override values in a Hibernate XML config file,
* or to specify all necessary properties locally.
* <p>Note: Do not specify a transaction provider here when using
* Spring-driven transactions. It is also advisable to omit connection
* provider settings and use a Spring-set DataSource instead.
* @see #setDataSource
*/
public
void
setHibernateProperties
(
Properties
hibernateProperties
)
{
this
.
hibernateProperties
=
hibernateProperties
;
}
/**
* Specify packages to search for autodetection of your entity classes in the
* classpath. This is analogous to Spring's component-scan feature
* ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
*/
public
void
setPackagesToScan
(
String
...
packagesToScan
)
{
this
.
packagesToScan
=
packagesToScan
;
}
/**
* Specify annotated entity classes to register with this Hibernate SessionFactory.
*/
public
void
setAnnotatedClasses
(
Class
<?>...
annotatedClasses
)
{
this
.
annotatedClasses
=
annotatedClasses
;
}
public
void
setResourceLoader
(
ResourceLoader
resourceLoader
)
{
this
.
resourcePatternResolver
=
ResourcePatternUtils
.
getResourcePatternResolver
(
resourceLoader
);
}
public
void
afterPropertiesSet
()
{
Configuration
config
=
new
Configuration
();
config
.
getProperties
().
put
(
Environment
.
CURRENT_SESSION_CONTEXT_CLASS
,
SpringSessionContext
.
class
.
getName
());
config
.
getProperties
().
put
(
Environment
.
DATASOURCE
,
this
.
dataSource
);
config
.
getProperties
().
put
(
"hibernate.classLoader.application"
,
this
.
resourcePatternResolver
.
getClassLoader
());
config
.
addProperties
(
this
.
hibernateProperties
);
scanPackages
(
config
);
for
(
Class
<?>
annotatedClass
:
this
.
annotatedClasses
)
{
ReflectionUtils
.
invokeMethod
(
addAnnotatedClassMethod
,
config
,
annotatedClass
);
}
this
.
sessionFactory
=
config
.
buildSessionFactory
();
}
/**
* Perform Spring-based scanning for entity classes.
* @see #setPackagesToScan
*/
private
void
scanPackages
(
Configuration
config
)
{
if
(
this
.
packagesToScan
!=
null
)
{
try
{
for
(
String
pkg
:
this
.
packagesToScan
)
{
String
pattern
=
ResourcePatternResolver
.
CLASSPATH_ALL_URL_PREFIX
+
ClassUtils
.
convertClassNameToResourcePath
(
pkg
)
+
RESOURCE_PATTERN
;
Resource
[]
resources
=
this
.
resourcePatternResolver
.
getResources
(
pattern
);
MetadataReaderFactory
readerFactory
=
new
CachingMetadataReaderFactory
(
this
.
resourcePatternResolver
);
for
(
Resource
resource
:
resources
)
{
if
(
resource
.
isReadable
())
{
MetadataReader
reader
=
readerFactory
.
getMetadataReader
(
resource
);
String
className
=
reader
.
getClassMetadata
().
getClassName
();
if
(
matchesFilter
(
reader
,
readerFactory
))
{
Class
<?>
annotatedClass
=
this
.
resourcePatternResolver
.
getClassLoader
().
loadClass
(
className
);
ReflectionUtils
.
invokeMethod
(
addAnnotatedClassMethod
,
config
,
annotatedClass
);
}
}
}
}
}
catch
(
IOException
ex
)
{
throw
new
MappingException
(
"Failed to scan classpath for unlisted classes"
,
ex
);
}
catch
(
ClassNotFoundException
ex
)
{
throw
new
MappingException
(
"Failed to load annotated classes from classpath"
,
ex
);
}
}
}
/**
* Check whether any of the configured entity type filters matches
* the current class descriptor contained in the metadata reader.
*/
private
boolean
matchesFilter
(
MetadataReader
reader
,
MetadataReaderFactory
readerFactory
)
throws
IOException
{
if
(
this
.
entityTypeFilters
!=
null
)
{
for
(
TypeFilter
filter
:
this
.
entityTypeFilters
)
{
if
(
filter
.
match
(
reader
,
readerFactory
))
{
return
true
;
}
}
}
return
false
;
}
public
SessionFactory
getObject
()
{
return
this
.
sessionFactory
;
}
public
Class
<?>
getObjectType
()
{
return
(
this
.
sessionFactory
!=
null
?
this
.
sessionFactory
.
getClass
()
:
SessionFactory
.
class
);
}
public
boolean
isSingleton
()
{
return
true
;
}
public
void
destroy
()
{
this
.
sessionFactory
.
close
();
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
javax.sql.DataSource
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.hibernate.HibernateException
;
import
org.hibernate.JDBCException
;
import
org.hibernate.NonUniqueResultException
;
import
org.hibernate.ObjectDeletedException
;
import
org.hibernate.PersistentObjectException
;
import
org.hibernate.PropertyValueException
;
import
org.hibernate.QueryException
;
import
org.hibernate.Session
;
import
org.hibernate.SessionFactory
;
import
org.hibernate.StaleObjectStateException
;
import
org.hibernate.StaleStateException
;
import
org.hibernate.TransientObjectException
;
import
org.hibernate.UnresolvableObjectException
;
import
org.hibernate.WrongClassException
;
import
org.hibernate.engine.spi.SessionFactoryImplementor
;
import
org.hibernate.exception.ConstraintViolationException
;
import
org.hibernate.exception.DataException
;
import
org.hibernate.exception.JDBCConnectionException
;
import
org.hibernate.exception.LockAcquisitionException
;
import
org.hibernate.exception.SQLGrammarException
;
import
org.hibernate.service.jdbc.connections.spi.ConnectionProvider
;
import
org.springframework.dao.CannotAcquireLockException
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.dao.DataAccessResourceFailureException
;
import
org.springframework.dao.DataIntegrityViolationException
;
import
org.springframework.dao.IncorrectResultSizeDataAccessException
;
import
org.springframework.dao.InvalidDataAccessApiUsageException
;
import
org.springframework.dao.InvalidDataAccessResourceUsageException
;
import
org.springframework.jdbc.datasource.DataSourceUtils
;
/**
* Helper class featuring methods for Hibernate Session handling.
* Also provides support for exception translation.
*
* <p>Used internally by {@link HibernateTransactionManager}.
* Can also be used directly in application code.
*
* @author Juergen Hoeller
* @since 3.1
* @see HibernateExceptionTranslator
* @see HibernateTransactionManager
*/
public
abstract
class
SessionFactoryUtils
{
/**
* Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
* Returns <code>DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100</code>
* to execute Session cleanup before JDBC Connection cleanup, if any.
* @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
*/
public
static
final
int
SESSION_SYNCHRONIZATION_ORDER
=
DataSourceUtils
.
CONNECTION_SYNCHRONIZATION_ORDER
-
100
;
static
final
Log
logger
=
LogFactory
.
getLog
(
SessionFactoryUtils
.
class
);
/**
* Determine the DataSource of the given SessionFactory.
* @param sessionFactory the SessionFactory to check
* @return the DataSource, or <code>null</code> if none found
* @see org.hibernate.engine.spi.SessionFactoryImplementor#getConnectionProvider
*/
public
static
DataSource
getDataSource
(
SessionFactory
sessionFactory
)
{
if
(
sessionFactory
instanceof
SessionFactoryImplementor
)
{
ConnectionProvider
cp
=
((
SessionFactoryImplementor
)
sessionFactory
).
getConnectionProvider
();
return
cp
.
unwrap
(
DataSource
.
class
);
}
return
null
;
}
/**
* Perform actual closing of the Hibernate Session,
* catching and logging any cleanup exceptions thrown.
* @param session the Hibernate Session to close (may be <code>null</code>)
* @see org.hibernate.Session#close()
*/
public
static
void
closeSession
(
Session
session
)
{
if
(
session
!=
null
)
{
try
{
session
.
close
();
}
catch
(
HibernateException
ex
)
{
logger
.
debug
(
"Could not close Hibernate Session"
,
ex
);
}
catch
(
Throwable
ex
)
{
logger
.
debug
(
"Unexpected exception on closing Hibernate Session"
,
ex
);
}
}
}
/**
* Convert the given HibernateException to an appropriate exception
* from the <code>org.springframework.dao</code> hierarchy.
* @param ex HibernateException that occured
* @return the corresponding DataAccessException instance
* @see HibernateExceptionTranslator#convertHibernateAccessException
* @see HibernateTransactionManager#convertHibernateAccessException
*/
public
static
DataAccessException
convertHibernateAccessException
(
HibernateException
ex
)
{
if
(
ex
instanceof
JDBCConnectionException
)
{
return
new
DataAccessResourceFailureException
(
ex
.
getMessage
(),
ex
);
}
if
(
ex
instanceof
SQLGrammarException
)
{
SQLGrammarException
jdbcEx
=
(
SQLGrammarException
)
ex
;
return
new
InvalidDataAccessResourceUsageException
(
ex
.
getMessage
()
+
"; SQL ["
+
jdbcEx
.
getSQL
()
+
"]"
,
ex
);
}
if
(
ex
instanceof
LockAcquisitionException
)
{
LockAcquisitionException
jdbcEx
=
(
LockAcquisitionException
)
ex
;
return
new
CannotAcquireLockException
(
ex
.
getMessage
()
+
"; SQL ["
+
jdbcEx
.
getSQL
()
+
"]"
,
ex
);
}
if
(
ex
instanceof
ConstraintViolationException
)
{
ConstraintViolationException
jdbcEx
=
(
ConstraintViolationException
)
ex
;
return
new
DataIntegrityViolationException
(
ex
.
getMessage
()
+
"; SQL ["
+
jdbcEx
.
getSQL
()
+
"]; constraint ["
+
jdbcEx
.
getConstraintName
()
+
"]"
,
ex
);
}
if
(
ex
instanceof
DataException
)
{
DataException
jdbcEx
=
(
DataException
)
ex
;
return
new
DataIntegrityViolationException
(
ex
.
getMessage
()
+
"; SQL ["
+
jdbcEx
.
getSQL
()
+
"]"
,
ex
);
}
if
(
ex
instanceof
JDBCException
)
{
return
new
HibernateJdbcException
((
JDBCException
)
ex
);
}
if
(
ex
instanceof
PropertyValueException
)
{
return
new
DataIntegrityViolationException
(
ex
.
getMessage
(),
ex
);
}
if
(
ex
instanceof
PersistentObjectException
)
{
return
new
InvalidDataAccessApiUsageException
(
ex
.
getMessage
(),
ex
);
}
if
(
ex
instanceof
TransientObjectException
)
{
return
new
InvalidDataAccessApiUsageException
(
ex
.
getMessage
(),
ex
);
}
if
(
ex
instanceof
ObjectDeletedException
)
{
return
new
InvalidDataAccessApiUsageException
(
ex
.
getMessage
(),
ex
);
}
if
(
ex
instanceof
QueryException
)
{
return
new
HibernateQueryException
((
QueryException
)
ex
);
}
if
(
ex
instanceof
UnresolvableObjectException
)
{
return
new
HibernateObjectRetrievalFailureException
((
UnresolvableObjectException
)
ex
);
}
if
(
ex
instanceof
WrongClassException
)
{
return
new
HibernateObjectRetrievalFailureException
((
WrongClassException
)
ex
);
}
if
(
ex
instanceof
NonUniqueResultException
)
{
return
new
IncorrectResultSizeDataAccessException
(
ex
.
getMessage
(),
1
,
ex
);
}
if
(
ex
instanceof
StaleObjectStateException
)
{
return
new
HibernateOptimisticLockingFailureException
((
StaleObjectStateException
)
ex
);
}
if
(
ex
instanceof
StaleStateException
)
{
return
new
HibernateOptimisticLockingFailureException
((
StaleStateException
)
ex
);
}
// fallback
return
new
HibernateSystemException
(
ex
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.FlushMode
;
import
org.hibernate.Session
;
import
org.hibernate.Transaction
;
import
org.springframework.transaction.support.ResourceHolderSupport
;
import
org.springframework.util.Assert
;
/**
* Session holder, wrapping a Hibernate Session and a Hibernate Transaction.
* HibernateTransactionManager binds instances of this class to the thread,
* for a given SessionFactory.
*
* <p>Note: This is an SPI class, not intended to be used by applications.
*
* @author Juergen Hoeller
* @since 3.1
* @see HibernateTransactionManager
* @see SessionFactoryUtils
*/
public
class
SessionHolder
extends
ResourceHolderSupport
{
private
Session
session
;
private
Transaction
transaction
;
private
FlushMode
previousFlushMode
;
public
SessionHolder
(
Session
session
)
{
Assert
.
notNull
(
session
,
"Session must not be null"
);
this
.
session
=
session
;
}
public
Session
getSession
()
{
return
this
.
session
;
}
public
void
setTransaction
(
Transaction
transaction
)
{
this
.
transaction
=
transaction
;
}
public
Transaction
getTransaction
()
{
return
this
.
transaction
;
}
public
void
setPreviousFlushMode
(
FlushMode
previousFlushMode
)
{
this
.
previousFlushMode
=
previousFlushMode
;
}
public
FlushMode
getPreviousFlushMode
()
{
return
this
.
previousFlushMode
;
}
@Override
public
void
clear
()
{
super
.
clear
();
this
.
session
=
null
;
this
.
transaction
=
null
;
this
.
previousFlushMode
=
null
;
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.springframework.transaction.support.TransactionSynchronizationAdapter
;
/**
* Simple synchronization adapter that propagates a <code>flush()</code> call
* to the underlying Hibernate Session. Used in combination with JTA.
*
* @author Juergen Hoeller
* @since 3.1
*/
class
SpringFlushSynchronization
extends
TransactionSynchronizationAdapter
{
private
final
Session
session
;
public
SpringFlushSynchronization
(
Session
session
)
{
this
.
session
=
session
;
}
@Override
public
void
flush
()
{
try
{
SessionFactoryUtils
.
logger
.
debug
(
"Flushing Hibernate Session on explicit request"
);
this
.
session
.
flush
();
}
catch
(
HibernateException
ex
)
{
throw
SessionFactoryUtils
.
convertHibernateAccessException
(
ex
);
}
}
@Override
public
boolean
equals
(
Object
obj
)
{
return
(
obj
instanceof
SpringFlushSynchronization
&&
this
.
session
==
((
SpringFlushSynchronization
)
obj
).
session
);
}
@Override
public
int
hashCode
()
{
return
this
.
session
.
hashCode
();
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.FlushMode
;
import
org.hibernate.Session
;
import
org.hibernate.context.internal.JTASessionContext
;
import
org.hibernate.engine.spi.SessionFactoryImplementor
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
/**
* Spring-specific subclass of Hibernate's JTASessionContext,
* setting <code>FlushMode.MANUAL</code> for read-only transactions.
*
* @author Juergen Hoeller
* @since 3.1
*/
public
class
SpringJtaSessionContext
extends
JTASessionContext
{
public
SpringJtaSessionContext
(
SessionFactoryImplementor
factory
)
{
super
(
factory
);
}
@Override
protected
Session
buildOrObtainSession
()
{
Session
session
=
super
.
buildOrObtainSession
();
if
(
TransactionSynchronizationManager
.
isCurrentTransactionReadOnly
())
{
session
.
setFlushMode
(
FlushMode
.
MANUAL
);
}
return
session
;
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
javax.transaction.TransactionManager
;
import
org.hibernate.FlushMode
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.hibernate.context.spi.CurrentSessionContext
;
import
org.hibernate.engine.spi.SessionFactoryImplementor
;
import
org.hibernate.service.jta.platform.spi.JtaPlatform
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
/**
* Implementation of Hibernate 3.1's CurrentSessionContext interface
* that delegates to Spring's SessionFactoryUtils for providing a
* Spring-managed current Session.
*
* <p>This CurrentSessionContext implementation can also be specified in custom
* SessionFactory setup through the "hibernate.current_session_context_class"
* property, with the fully qualified name of this class as value.
*
* @author Juergen Hoeller
* @since 3.1
*/
@SuppressWarnings
(
"serial"
)
public
class
SpringSessionContext
implements
CurrentSessionContext
{
private
final
SessionFactoryImplementor
sessionFactory
;
private
final
CurrentSessionContext
jtaSessionContext
;
/**
* Create a new SpringSessionContext for the given Hibernate SessionFactory.
* @param sessionFactory the SessionFactory to provide current Sessions for
*/
public
SpringSessionContext
(
SessionFactoryImplementor
sessionFactory
)
{
this
.
sessionFactory
=
sessionFactory
;
JtaPlatform
jtaPlatform
=
sessionFactory
.
getServiceRegistry
().
getService
(
JtaPlatform
.
class
);
TransactionManager
transactionManager
=
jtaPlatform
.
retrieveTransactionManager
();
this
.
jtaSessionContext
=
(
transactionManager
!=
null
?
new
SpringJtaSessionContext
(
sessionFactory
)
:
null
);
}
/**
* Retrieve the Spring-managed Session for the current thread, if any.
*/
public
Session
currentSession
()
throws
HibernateException
{
Object
value
=
TransactionSynchronizationManager
.
getResource
(
this
.
sessionFactory
);
if
(
value
instanceof
Session
)
{
return
(
Session
)
value
;
}
else
if
(
value
instanceof
SessionHolder
)
{
SessionHolder
sessionHolder
=
(
SessionHolder
)
value
;
Session
session
=
sessionHolder
.
getSession
();
if
(
TransactionSynchronizationManager
.
isSynchronizationActive
()
&&
!
sessionHolder
.
isSynchronizedWithTransaction
())
{
TransactionSynchronizationManager
.
registerSynchronization
(
new
SpringSessionSynchronization
(
sessionHolder
,
this
.
sessionFactory
));
sessionHolder
.
setSynchronizedWithTransaction
(
true
);
// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
// with FlushMode.MANUAL, which needs to allow flushing within the transaction.
FlushMode
flushMode
=
session
.
getFlushMode
();
if
(
FlushMode
.
isManualFlushMode
(
flushMode
)
&&
!
TransactionSynchronizationManager
.
isCurrentTransactionReadOnly
())
{
session
.
setFlushMode
(
FlushMode
.
AUTO
);
sessionHolder
.
setPreviousFlushMode
(
flushMode
);
}
}
return
session
;
}
else
if
(
this
.
jtaSessionContext
!=
null
)
{
Session
session
=
this
.
jtaSessionContext
.
currentSession
();
if
(
TransactionSynchronizationManager
.
isSynchronizationActive
())
{
TransactionSynchronizationManager
.
registerSynchronization
(
new
SpringFlushSynchronization
(
session
));
}
return
session
;
}
else
{
throw
new
HibernateException
(
"No Session found for current thread"
);
}
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4
;
import
org.hibernate.FlushMode
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.hibernate.SessionFactory
;
import
org.springframework.core.Ordered
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.transaction.support.TransactionSynchronization
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
/**
* Callback for resource cleanup at the end of a Spring-managed transaction
* for a pre-bound Hibernate Session.
*
* @author Juergen Hoeller
* @since 3.1
*/
class
SpringSessionSynchronization
implements
TransactionSynchronization
,
Ordered
{
private
final
SessionHolder
sessionHolder
;
private
final
SessionFactory
sessionFactory
;
private
boolean
holderActive
=
true
;
public
SpringSessionSynchronization
(
SessionHolder
sessionHolder
,
SessionFactory
sessionFactory
)
{
this
.
sessionHolder
=
sessionHolder
;
this
.
sessionFactory
=
sessionFactory
;
}
private
Session
getCurrentSession
()
{
return
this
.
sessionHolder
.
getSession
();
}
public
int
getOrder
()
{
return
SessionFactoryUtils
.
SESSION_SYNCHRONIZATION_ORDER
;
}
public
void
suspend
()
{
if
(
this
.
holderActive
)
{
TransactionSynchronizationManager
.
unbindResource
(
this
.
sessionFactory
);
// Eagerly disconnect the Session here, to make release mode "on_close" work on JBoss.
getCurrentSession
().
disconnect
();
}
}
public
void
resume
()
{
if
(
this
.
holderActive
)
{
TransactionSynchronizationManager
.
bindResource
(
this
.
sessionFactory
,
this
.
sessionHolder
);
}
}
public
void
flush
()
{
try
{
SessionFactoryUtils
.
logger
.
debug
(
"Flushing Hibernate Session on explicit request"
);
getCurrentSession
().
flush
();
}
catch
(
HibernateException
ex
)
{
throw
SessionFactoryUtils
.
convertHibernateAccessException
(
ex
);
}
}
public
void
beforeCommit
(
boolean
readOnly
)
throws
DataAccessException
{
if
(!
readOnly
)
{
Session
session
=
getCurrentSession
();
// Read-write transaction -> flush the Hibernate Session.
// Further check: only flush when not FlushMode.MANUAL.
if
(!
FlushMode
.
isManualFlushMode
(
session
.
getFlushMode
()))
{
try
{
SessionFactoryUtils
.
logger
.
debug
(
"Flushing Hibernate Session on transaction synchronization"
);
session
.
flush
();
}
catch
(
HibernateException
ex
)
{
throw
SessionFactoryUtils
.
convertHibernateAccessException
(
ex
);
}
}
}
}
public
void
beforeCompletion
()
{
Session
session
=
this
.
sessionHolder
.
getSession
();
if
(
this
.
sessionHolder
.
getPreviousFlushMode
()
!=
null
)
{
// In case of pre-bound Session, restore previous flush mode.
session
.
setFlushMode
(
this
.
sessionHolder
.
getPreviousFlushMode
());
}
// Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
session
.
disconnect
();
}
public
void
afterCommit
()
{
}
public
void
afterCompletion
(
int
status
)
{
if
(
status
!=
STATUS_COMMITTED
)
{
// Clear all pending inserts/updates/deletes in the Session.
// Necessary for pre-bound Sessions, to avoid inconsistent state.
this
.
sessionHolder
.
getSession
().
clear
();
}
this
.
sessionHolder
.
setSynchronizedWithTransaction
(
false
);
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/package-info.java
0 → 100644
浏览文件 @
0ff83606
/**
*
* Package providing integration of
* <a href="http://www.hibernate.org">Hibernate 4.0</a>
* with Spring concepts.
*
* <p>Contains an implementation of Spring's transaction SPI for local Hibernate transactions.
* This package is intentionally rather minimal, relying on native Hibernate builder APIs
* for building a SessionFactory (for example in an @Bean method in a @Configuration class).
*
* <p><b>This package supports Hibernate 4.x only.</b>
* See the org.springframework.orm.hibernate3 package for Hibernate 3.x support.
*
*/
package
org.springframework.orm.hibernate4
;
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4.support
;
import
java.io.IOException
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.hibernate.FlushMode
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.hibernate.SessionFactory
;
import
org.springframework.dao.DataAccessResourceFailureException
;
import
org.springframework.orm.hibernate4.SessionFactoryUtils
;
import
org.springframework.orm.hibernate4.SessionHolder
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.context.support.WebApplicationContextUtils
;
import
org.springframework.web.filter.OncePerRequestFilter
;
/**
* Servlet 2.3 Filter that binds a Hibernate Session to the thread for the entire
* processing of the request. Intended for the "Open Session in View" pattern,
* i.e. to allow for lazy loading in web views despite the original transactions
* already being completed.
*
* <p>This filter makes Hibernate Sessions available via the current thread, which
* will be autodetected by transaction managers. It is suitable for service layer
* transactions via {@link org.springframework.orm.hibernate4.HibernateTransactionManager}
* as well as for non-transactional execution (if configured appropriately).
*
* <p><b>NOTE</b>: This filter will by default <i>not</i> flush the Hibernate Session,
* with the flush mode set to <code>FlushMode.NEVER</code>. It assumes to be used
* in combination with service layer transactions that care for the flushing: The
* active transaction manager will temporarily change the flush mode to
* <code>FlushMode.AUTO</code> during a read-write transaction, with the flush
* mode reset to <code>FlushMode.NEVER</code> at the end of each transaction.
*
* <p><b>WARNING:</b> Applying this filter to existing logic can cause issues that
* have not appeared before, through the use of a single Hibernate Session for the
* processing of an entire request. In particular, the reassociation of persistent
* objects with a Hibernate Session has to occur at the very beginning of request
* processing, to avoid clashes with already loaded instances of the same objects.
*
* <p>Looks up the SessionFactory in Spring's root web application context.
* Supports a "sessionFactoryBeanName" filter init-param in <code>web.xml</code>;
* the default bean name is "sessionFactory". Looks up the SessionFactory on each
* request, to avoid initialization order issues (when using ContextLoaderServlet,
* the root application context will get initialized <i>after</i> this filter).
*
* @author Juergen Hoeller
* @since 3.1
* @see #lookupSessionFactory
* @see OpenSessionInViewInterceptor
* @see org.springframework.orm.hibernate4.HibernateTransactionManager
* @see org.springframework.transaction.support.TransactionSynchronizationManager
*/
public
class
OpenSessionInViewFilter
extends
OncePerRequestFilter
{
public
static
final
String
DEFAULT_SESSION_FACTORY_BEAN_NAME
=
"sessionFactory"
;
private
String
sessionFactoryBeanName
=
DEFAULT_SESSION_FACTORY_BEAN_NAME
;
/**
* Set the bean name of the SessionFactory to fetch from Spring's
* root application context. Default is "sessionFactory".
* @see #DEFAULT_SESSION_FACTORY_BEAN_NAME
*/
public
void
setSessionFactoryBeanName
(
String
sessionFactoryBeanName
)
{
this
.
sessionFactoryBeanName
=
sessionFactoryBeanName
;
}
/**
* Return the bean name of the SessionFactory to fetch from Spring's
* root application context.
*/
protected
String
getSessionFactoryBeanName
()
{
return
this
.
sessionFactoryBeanName
;
}
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
SessionFactory
sessionFactory
=
lookupSessionFactory
(
request
);
boolean
participate
=
false
;
if
(
TransactionSynchronizationManager
.
hasResource
(
sessionFactory
))
{
// Do not modify the Session: just set the participate flag.
participate
=
true
;
}
else
{
logger
.
debug
(
"Opening Hibernate Session in OpenSessionInViewFilter"
);
Session
session
=
openSession
(
sessionFactory
);
TransactionSynchronizationManager
.
bindResource
(
sessionFactory
,
new
SessionHolder
(
session
));
}
try
{
filterChain
.
doFilter
(
request
,
response
);
}
finally
{
if
(!
participate
)
{
SessionHolder
sessionHolder
=
(
SessionHolder
)
TransactionSynchronizationManager
.
unbindResource
(
sessionFactory
);
logger
.
debug
(
"Closing Hibernate Session in OpenSessionInViewFilter"
);
SessionFactoryUtils
.
closeSession
(
sessionHolder
.
getSession
());
}
}
}
/**
* Look up the SessionFactory that this filter should use,
* taking the current HTTP request as argument.
* <p>The default implementation delegates to the {@link #lookupSessionFactory()}
* variant without arguments.
* @param request the current request
* @return the SessionFactory to use
*/
protected
SessionFactory
lookupSessionFactory
(
HttpServletRequest
request
)
{
return
lookupSessionFactory
();
}
/**
* Look up the SessionFactory that this filter should use.
* <p>The default implementation looks for a bean with the specified name
* in Spring's root application context.
* @return the SessionFactory to use
* @see #getSessionFactoryBeanName
*/
protected
SessionFactory
lookupSessionFactory
()
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Using SessionFactory '"
+
getSessionFactoryBeanName
()
+
"' for OpenSessionInViewFilter"
);
}
WebApplicationContext
wac
=
WebApplicationContextUtils
.
getRequiredWebApplicationContext
(
getServletContext
());
return
wac
.
getBean
(
getSessionFactoryBeanName
(),
SessionFactory
.
class
);
}
/**
* Open a Session for the SessionFactory that this filter uses.
* <p>The default implementation delegates to the
* <code>SessionFactory.openSession</code> method and
* sets the <code>Session</code>'s flush mode to "MANUAL".
* @param sessionFactory the SessionFactory that this filter uses
* @return the Session to use
* @throws DataAccessResourceFailureException if the Session could not be created
* @see org.hibernate.FlushMode#MANUAL
*/
protected
Session
openSession
(
SessionFactory
sessionFactory
)
throws
DataAccessResourceFailureException
{
try
{
Session
session
=
sessionFactory
.
openSession
();
session
.
setFlushMode
(
FlushMode
.
MANUAL
);
return
session
;
}
catch
(
HibernateException
ex
)
{
throw
new
DataAccessResourceFailureException
(
"Could not open Hibernate Session"
,
ex
);
}
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.orm.hibernate4.support
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.hibernate.FlushMode
;
import
org.hibernate.HibernateException
;
import
org.hibernate.Session
;
import
org.hibernate.SessionFactory
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.dao.DataAccessResourceFailureException
;
import
org.springframework.orm.hibernate4.SessionFactoryUtils
;
import
org.springframework.orm.hibernate4.SessionHolder
;
import
org.springframework.transaction.support.TransactionSynchronizationManager
;
import
org.springframework.ui.ModelMap
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.context.request.WebRequestInterceptor
;
/**
* Spring web request interceptor that binds a Hibernate <code>Session</code> to the
* thread for the entire processing of the request.
*
* <p>This class is a concrete expression of the "Open Session in View" pattern, which
* is a pattern that allows for the lazy loading of associations in web views despite
* the original transactions already being completed.
*
* <p>This interceptor makes Hibernate Sessions available via the current thread,
* which will be autodetected by transaction managers. It is suitable for service layer
* transactions via {@link org.springframework.orm.hibernate4.HibernateTransactionManager}
* as well as for non-transactional execution (if configured appropriately).
*
* <p><b>NOTE</b>: This interceptor will by default <i>not</i> flush the Hibernate
* <code>Session</code>, with the flush mode being set to <code>FlushMode.NEVER</code>.
* It assumes that it will be used in combination with service layer transactions
* that handle the flushing: the active transaction manager will temporarily change
* the flush mode to <code>FlushMode.AUTO</code> during a read-write transaction,
* with the flush mode reset to <code>FlushMode.NEVER</code> at the end of each
* transaction. If you intend to use this interceptor without transactions, consider
* changing the default flush mode (through the {@link #setFlushMode "flushMode"} property).
*
* <p>In contrast to {@link OpenSessionInViewFilter}, this interceptor is configured
* in a Spring application context and can thus take advantage of bean wiring..
*
* <p><b>WARNING:</b> Applying this interceptor to existing logic can cause issues
* that have not appeared before, through the use of a single Hibernate
* <code>Session</code> for the processing of an entire request. In particular, the
* reassociation of persistent objects with a Hibernate <code>Session</code> has to
* occur at the very beginning of request processing, to avoid clashes with already
* loaded instances of the same objects.
*
* @author Juergen Hoeller
* @since 3.1
* @see #setSingleSession
* @see #setFlushMode
* @see OpenSessionInViewFilter
* @see org.springframework.orm.hibernate4.HibernateTransactionManager
* @see org.springframework.transaction.support.TransactionSynchronizationManager
*/
public
class
OpenSessionInViewInterceptor
implements
WebRequestInterceptor
{
/**
* Suffix that gets appended to the <code>SessionFactory</code>
* <code>toString()</code> representation for the "participate in existing
* session handling" request attribute.
* @see #getParticipateAttributeName
*/
public
static
final
String
PARTICIPATE_SUFFIX
=
".PARTICIPATE"
;
protected
final
Log
logger
=
LogFactory
.
getLog
(
getClass
());
private
SessionFactory
sessionFactory
;
public
void
setSessionFactory
(
SessionFactory
sessionFactory
)
{
this
.
sessionFactory
=
sessionFactory
;
}
public
SessionFactory
getSessionFactory
()
{
return
this
.
sessionFactory
;
}
/**
* Open a new Hibernate <code>Session</code> according to the settings of this
* <code>HibernateAccessor</code> and bind it to the thread via the
* {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
*/
public
void
preHandle
(
WebRequest
request
)
throws
DataAccessException
{
if
(
TransactionSynchronizationManager
.
hasResource
(
getSessionFactory
()))
{
// Do not modify the Session: just mark the request accordingly.
String
participateAttributeName
=
getParticipateAttributeName
();
Integer
count
=
(
Integer
)
request
.
getAttribute
(
participateAttributeName
,
WebRequest
.
SCOPE_REQUEST
);
int
newCount
=
(
count
!=
null
?
count
+
1
:
1
);
request
.
setAttribute
(
getParticipateAttributeName
(),
newCount
,
WebRequest
.
SCOPE_REQUEST
);
}
else
{
logger
.
debug
(
"Opening Hibernate Session in OpenSessionInViewInterceptor"
);
Session
session
=
openSession
();
TransactionSynchronizationManager
.
bindResource
(
getSessionFactory
(),
new
SessionHolder
(
session
));
}
}
public
void
postHandle
(
WebRequest
request
,
ModelMap
model
)
{
}
/**
* Unbind the Hibernate <code>Session</code> from the thread and close it).
* @see org.springframework.transaction.support.TransactionSynchronizationManager
*/
public
void
afterCompletion
(
WebRequest
request
,
Exception
ex
)
throws
DataAccessException
{
String
participateAttributeName
=
getParticipateAttributeName
();
Integer
count
=
(
Integer
)
request
.
getAttribute
(
participateAttributeName
,
WebRequest
.
SCOPE_REQUEST
);
if
(
count
!=
null
)
{
// Do not modify the Session: just clear the marker.
if
(
count
>
1
)
{
request
.
setAttribute
(
participateAttributeName
,
count
-
1
,
WebRequest
.
SCOPE_REQUEST
);
}
else
{
request
.
removeAttribute
(
participateAttributeName
,
WebRequest
.
SCOPE_REQUEST
);
}
}
else
{
SessionHolder
sessionHolder
=
(
SessionHolder
)
TransactionSynchronizationManager
.
unbindResource
(
getSessionFactory
());
logger
.
debug
(
"Closing Hibernate Session in OpenSessionInViewInterceptor"
);
SessionFactoryUtils
.
closeSession
(
sessionHolder
.
getSession
());
}
}
/**
* Open a Session for the SessionFactory that this interceptor uses.
* <p>The default implementation delegates to the
* <code>SessionFactory.openSession</code> method and
* sets the <code>Session</code>'s flush mode to "MANUAL".
* @return the Session to use
* @throws DataAccessResourceFailureException if the Session could not be created
* @see org.hibernate.FlushMode#MANUAL
*/
protected
Session
openSession
()
throws
DataAccessResourceFailureException
{
try
{
Session
session
=
getSessionFactory
().
openSession
();
session
.
setFlushMode
(
FlushMode
.
MANUAL
);
return
session
;
}
catch
(
HibernateException
ex
)
{
throw
new
DataAccessResourceFailureException
(
"Could not open Hibernate Session"
,
ex
);
}
}
/**
* Return the name of the request attribute that identifies that a request is
* already intercepted.
* <p>The default implementation takes the <code>toString()</code> representation
* of the <code>SessionFactory</code> instance and appends {@link #PARTICIPATE_SUFFIX}.
*/
protected
String
getParticipateAttributeName
()
{
return
getSessionFactory
().
toString
()
+
PARTICIPATE_SUFFIX
;
}
}
org.springframework.orm/src/main/java/org/springframework/orm/hibernate4/support/package-info.java
0 → 100644
浏览文件 @
0ff83606
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
* Classes supporting the <code>org.springframework.orm.hibernate4</code> package.
*
*/
package
org.springframework.orm.hibernate4.support
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录