<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是 webx.xml 过滤器设置
在我的自定义过滤器中:
public class CustomSettingsFilter extends OncePerRequestFilter {
}
已经从 jpa 会话加载了一个实体,我把它放在了 ThreadLocal 中。
但是在spring mvc控制器出现异常的时候是由
"org.hibernate.PropertyValueException: not-null property references a null or transient value: net.shopxx.entity.Bom.member"
我没有发现这个异常。
然后它处理
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="shop/common/error" />
<property name="warnLogCategory">
<value>
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
</value>
</property>
</bean>
抛出一个新异常:
Template processing error: "get(currencyDescriptions) failed on instance of common.i18n.entity.Currency_$$_javassist_6"
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
common.i18n.entity.Currency_$$_javassist_6" 我在 ThreadLocal 中持有的这个对象
异常日志:
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: net.shopxx.entity.Bom.member
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:103)
at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:69)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875)
... 85 more
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47123 ] - [ DEBUG ] Returning cached instance of singleton bean 'freeMarkerConfigurer'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47124 ] - [ DEBUG ] Invoking afterPropertiesSet() on bean with name 'shop/common/error'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47127 ] - [ DEBUG ] Rendering view [org.springframework.web.servlet.view.freemarker.FreeMarkerView: name 'shop/common/error'; URL [shop/common/error.ftl]] in DispatcherServlet with name 'springmvc'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47128 ] - [ DEBUG ] Added model object 'springMacroRequestContext' of type [org.springframework.web.servlet.support.RequestContext] to request in view with name 'shop/common/error'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47128 ] - [ DEBUG ] Added model object 'exception' of type [javax.persistence.PersistenceException] to request in view with name 'shop/common/error'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47129 ] - [ DEBUG ] Rendering FreeMarker template [shop/common/error.ftl] in FreeMarkerView 'shop/common/error'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47130 ] - [ DEBUG ] No properties file found for [/WEB-INF/language/common/message_en_uk] - neither plain properties nor XML
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47130 ] - [ DEBUG ] Re-caching properties for filename [/WEB-INF/language/common/message_zh_CN] - file hasn't been modified
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47131 ] - [ DEBUG ] No properties file found for [/WEB-INF/language/common/message_zh] - neither plain properties nor XML
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47131 ] - [ DEBUG ] No properties file found for [/WEB-INF/language/common/message] - neither plain properties nor XML
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47132 ] - [ DEBUG ] No properties file found for [/WEB-INF/language/shop/message_en_uk] - neither plain properties nor XML
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47132 ] - [ DEBUG ] Re-caching properties for filename [/WEB-INF/language/shop/message_zh_CN] - file hasn't been modified
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47133 ] - [ DEBUG ] Returning cached instance of singleton bean 'transactionManager'
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47134 ] - [ DEBUG ] Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@73494c99] for JPA transaction
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47134 ] - [ DEBUG ] Creating new transaction with name [net.shopxx.service.impl.LocalInfoServiceImpl.findAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47135 ] - [ DEBUG ] begin
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47135 ] - [ DEBUG ] initial autocommit status: true
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47136 ] - [ DEBUG ] disabling autocommit
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47136 ] - [ DEBUG ] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@2aa4071a]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47137 ] - [ DEBUG ] Rendered criteria query -> select generatedAlias0 from LocalInfo as generatedAlias0 order by generatedAlias0.order asc
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47138 ] - [ DEBUG ] select localinfo0_.id as id1_2_, localinfo0_.create_gmt as create2_2_, localinfo0_.is_deleted as is3_2_, localinfo0_.modify_gmt as modify4_2_, localinfo0_.orders as orders5_2_, localinfo0_.country as country6_2_, localinfo0_.currency as currenc18_2_, localinfo0_.decimal_format as decimal7_2_, localinfo0_.decimal_separator as decimal8_2_, localinfo0_.digit as digit9_2_, localinfo0_.grouping_separator as groupin10_2_, localinfo0_.icon as icon11_2_, localinfo0_.is_default as is12_2_, localinfo0_.is_enable as is13_2_, localinfo0_.lan as lan14_2_, localinfo0_.language as languag15_2_, localinfo0_.local_code as local16_2_, localinfo0_.short_country as short17_2_, localinfo0_.time_zone as time19_2_ from i18n_localinfo localinfo0_ order by localinfo0_.orders asc
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47139 ] - [ DEBUG ] Result set row: 0
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47140 ] - [ DEBUG ] Result row: EntityKey[common.i18n.entity.LocalInfo#1]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47141 ] - [ DEBUG ] Result set row: 1
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47141 ] - [ DEBUG ] Result row: EntityKey[common.i18n.entity.LocalInfo#2]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47142 ] - [ DEBUG ] Resolving associations for [common.i18n.entity.LocalInfo#1]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47142 ] - [ DEBUG ] Done materializing entity [common.i18n.entity.LocalInfo#1]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47143 ] - [ DEBUG ] Resolving associations for [common.i18n.entity.LocalInfo#2]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47143 ] - [ DEBUG ] Done materializing entity [common.i18n.entity.LocalInfo#2]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47144 ] - [ DEBUG ] Initiating transaction commit
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47144 ] - [ DEBUG ] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@73494c99]
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47145 ] - [ DEBUG ] committing
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47145 ] - [ DEBUG ] committed JDBC Connection
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47146 ] - [ DEBUG ] re-enabling autocommit
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47146 ] - [ DEBUG ] Not closing pre-bound JPA EntityManager after transaction
2013-07-23 15:59:28 [ http-apr-8080-exec-4:47148 ] - [ ERROR ] Template processing error: "get(currencyDescriptions) failed on instance of common.i18n.entity.Currency_$$_javassist_6"
我在下面做一些事情:
- 注释异常代码。在控制器中抛出运行时异常,没有出现无会话异常;
- 如果我通过 try catch 捕获 jpa 异常,并且也没有出现没有会话异常
请帮助我。谢谢大家