参考文章:
http://stackoverflow.com/questions/1368025/grails-and-hibernates-lazy-initialization-exception
http://piperzero.iteye.com/blog/1660556
错误信息:
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.grails.orm.hibernate.proxy.SimpleHibernateProxyHandler.unwrapProxy(SimpleHibernateProxyHandler.java:79)
at org.grails.orm.hibernate.cfg.GrailsHibernateUtil.unwrapProxy(GrailsHibernateUtil.java:391)
at org.grails.orm.hibernate.cfg.HibernateUtils$_handleLazyProxy_closure1.doCall(HibernateUtils.groovy:48)
at sun.reflect.GeneratedMethodAccessor327.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1432)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:84)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1849)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1155)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3734)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1167)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassGetPropertySite.getProperty(PogoMetaClassGetPropertySite.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
-----------------------------------------------------------------------------------------
解决方法:
国外的:
Lets take an example :
class Book {
String title
Author author
}
class Author {
...
}
Book book = Book.get(1)
As we know, default fetch mode is lazy in domain classes. Considering the above example, say we get the book object, then Book object gets attached to the hibernate session object i.e first level cache automatically. And after the domain object gets detached from the session object and then we try to fetch book.author, at this moment it raises the Lazy initialization exception.
So the solution is, to have either fetch mode as eager or attach your book object to hibernate session using the code given below :
if(!book.isAttached()){
book.attach()
}
The description given above is one of the scenario. There could be many more. I request others to please share.
--------------------------------------------------------------------------------------------------------------------------
国内的:
第一种方式:
static mapping = {
acquirerAccount fetch:"join"
}
static belongsTo = [acquirerAccount:boss.BoAcquirerAccount]
第二种方式:
static mapping = {
acquirerAccount lazy:false //也可以这样去配置
}
还有一种方式:
static mapping = {
table 'BO_MERCHANT'
cache usage:'read-only', include:'non-lazy'//使用非lazy方式的配置(来自grails参考手册)
//cache:'read-write' // or 'read-only' or 'transactional'
}
http://stackoverflow.com/questions/1368025/grails-and-hibernates-lazy-initialization-exception
http://piperzero.iteye.com/blog/1660556
错误信息:
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.grails.orm.hibernate.proxy.SimpleHibernateProxyHandler.unwrapProxy(SimpleHibernateProxyHandler.java:79)
at org.grails.orm.hibernate.cfg.GrailsHibernateUtil.unwrapProxy(GrailsHibernateUtil.java:391)
at org.grails.orm.hibernate.cfg.HibernateUtils$_handleLazyProxy_closure1.doCall(HibernateUtils.groovy:48)
at sun.reflect.GeneratedMethodAccessor327.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1432)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:84)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1849)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1155)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3734)
at groovy.lang.ExpandoMetaClass.getProperty(ExpandoMetaClass.java:1167)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassGetPropertySite.getProperty(PogoMetaClassGetPropertySite.java:51)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
-----------------------------------------------------------------------------------------
解决方法:
国外的:
Lets take an example :
class Book {
String title
Author author
}
class Author {
...
}
Book book = Book.get(1)
As we know, default fetch mode is lazy in domain classes. Considering the above example, say we get the book object, then Book object gets attached to the hibernate session object i.e first level cache automatically. And after the domain object gets detached from the session object and then we try to fetch book.author, at this moment it raises the Lazy initialization exception.
So the solution is, to have either fetch mode as eager or attach your book object to hibernate session using the code given below :
if(!book.isAttached()){
book.attach()
}
The description given above is one of the scenario. There could be many more. I request others to please share.
--------------------------------------------------------------------------------------------------------------------------
国内的:
第一种方式:
static mapping = {
acquirerAccount fetch:"join"
}
static belongsTo = [acquirerAccount:boss.BoAcquirerAccount]
第二种方式:
static mapping = {
acquirerAccount lazy:false //也可以这样去配置
}
还有一种方式:
static mapping = {
table 'BO_MERCHANT'
cache usage:'read-only', include:'non-lazy'//使用非lazy方式的配置(来自grails参考手册)
//cache:'read-write' // or 'read-only' or 'transactional'
}