简体   繁体   English

grails在事务服务中获得lazzy初始化异常

[英]grails getting lazzy initialization exception in transactional service

I'm getting a typical hibernate Lazy initialization exception in my transactional service while accessing propert of an object of a SET. 我在访问SET对象的属性时,在我的事务服务中得到一个典型的hibernate Lazy初始化异常。

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


class ProductService {
  static transactional = true
  def xyz() {
    def products = Product.list()
    products.each { product ->
      def category = product.categories.asList().first()
      def title = category.title
    }
  }
}

I'm getting exception when accessing the title property of category 我在访问类别的title属性时遇到异常

xyz is a Closure being invoked by Groovy as if it were a method, but it's not a method. xyz是一个由Groovy调用的Closure,好像它是一个方法,但它不是一个方法。 So it's not proxied by Spring (it's just a field), and you get no transactional behavior. 所以它不是由Spring代理的(它只是一个字段),而且你没有任何事务行为。 There's almost never a good reason to have a public closure in a Service class (inner utility closures are fine). 几乎没有理由在Service类中使用公共闭包(内部实用程序闭包很好)。

This should work: 这应该工作:

void xyz() {
   for (product in Product.list()) {
       def category = product.categories.asList().first()
       def title = category.title
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM