【问题标题】:Grails - java.lang.IllegalStateException: Proxy could not be initializedGrails - java.lang.IllegalStateException:无法初始化代理
【发布时间】:2015-08-06 14:59:19
【问题描述】:

我有一个名为 InventoryLineItem 的域类,它有一个 InventoryAccess 类型的属性:

class InventoryLineItem {

    InventoryAccess inventoryAccess
    ...
}

还有一个名为 LineItem 的域类具有 InventoryLineItem 类型的属性:

class LineItem {

    InventoryLineItem inventoryLineItem
    ...
}

我面临的问题是,当我尝试从 LineItem 类(例如inventoryLineItem.inventoryAccess?.allotment)访问 InventoryAccess 的任何属性时,出现以下异常:

java.lang.IllegalStateException: Proxy for [com.acme.inventory.domain.InventoryAccess:8604205056156879654] could not be initialized

我将实例获取到 LineItem 实例,如下所示:

List<LineItem> lineItemsPendingTransfer = originalDeliveredItemIds.collect { Long id ->
    InventoryLineItem.get(id)?.lineItem
}

然后,我遍历每个订单项,并为每个订单项调用 LineItem 中的一个方法来执行此操作:

if (inventoryLineItem.inventoryAccess?.allotment?.scannableEndDate?.before(new Date())) {
    return LineItemTransferState.buildUnavailableState(UnavailableTransferReason.OUTDATED)
}

这就是问题发生的地方。一旦我尝试从inventoryAccess 对象中获取allotment 属性,就会引发异常。

我尝试将 InventoryLineItem 中的 inventoryAccess 属性设置为 lazy: false,但这没有帮助。

【问题讨论】:

  • 您需要为您的问题提供更多背景信息。您试图在您的应用程序中的哪个位置访问它?你是如何获得inventoryLineItem 的实例的?这是在控制器中吗?服务?标签库?包含应用程序该部分的代码。
  • 我在原始消息中添加了一些上下文。感谢您的帮助。
  • InventoryAccess 是域类吗?

标签: hibernate grails grails-orm grails-2.0


【解决方案1】:

如果 InventoryAccess 应该被持久化在数据库中,那么它也需要是一个域类。

如果不是,那么您可以将其设为transient,但您必须自己设置该值。例如,您可以添加一个 getter 来延迟初始化属性:

class InventoryLineItem {
    InventoryAccess inventoryAccess

    static transients = ['inventoryAccess']

    InventoryAccess getInventoryAccess() {
        if(inventoryAccess == null) {
            // do something to initialize inventoryAcces
        }

        return inventoryAccess
    }
}

【讨论】:

  • InventoryAccess 是一个域类。我最终更改了一些代码,一切都很好,但我仍然不完全理解为什么事情没有按照我最初编码的方式工作。
猜你喜欢
  • 2013-04-03
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
相关资源
最近更新 更多