grails org.hibernate.LazyInitializationException: could not initialize proxy - no Session

参考文章:
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'
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

friendan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值