Hibernate延迟加载代理 - GORM Static Api的`instanceOf`抛出ClassCastException

时间:2014-10-06 16:04:42

标签: hibernate grails groovy gorm

我在groovy / grails中重构现有系统的遗留代码。我没有使用.class.name来检查类类型,而是希望使用代理安全且更简洁的.instanceOf(尽管长期目标是完全摆脱那些类型检查)。

EDIT 2014/10/07:转向静态instanceOf方法不仅仅是为了更简洁的代码;这是不必担心由延迟加载的实例导致的hibernate代理。下面的服务方法可以接收域类实例或hibernate代理作为参数。

大多数情况下,在迭代共享超类的对象列表时会使用类名检查。这是一个简化的例子:

class PartnerContract{
    Partner partner
    String serialCode
    Date startDate
    Date endDate
}

class ServiceContract extends PartnerContract{
    BigDecimal payRate
}

class SpecialOrderContract extends PartnerContract
    BigDecimal wage
}

常见的操作是查找适合某个合作伙伴合同的所有客户合同(在域示例中未指定)。

在服务方法中,它的实现类似于:

    ...
    def pc = []
    partnerContracts.each{pv ->
      (getCustomerContracts(pv).collect(pc){it}
    }    
    ...

def getCustomerContracts(PartnerContract partnerContractInstance){
    def customerContracts = []

    if(partnerContractInstance.instanceOf(ServiceContract)){    
        //find contracts and add to customerContracts
    }
    else if(partnerContractInstance.instanceOf(SpecialOrderContract)){
        //find contracts and add to customerContracts
    }
    customerContracts
}

在我的本地环境中,一切都按预期工作。 (编辑2014/10/07:run-wartest run-warprod run-war工作正常。)这不是我期待任何问题的变化从根本上说,它似乎相当微不足道。但是,奇怪的是,在将WAR文件部署到服务器时,会抛出以下异常:

抛出以下异常:

com.itf.propart.PartnerContract_$$_javassist_157 cannot be cast to com.itf.propart.ServiceContract

在我看来,无论我使用instanceOf做错了什么,它在本地环境中工作都是非常误导的,但在构建WAR文件时却没有。我不明白为什么它在任何情况下都应该起作用,如果不正确的话。感觉非常错。

EDIT 2014/10/07:我在server.log找到了可能相关的内容。大多数(甚至可能是所有)域类都抛出此异常。 DienstleistungsVertragTMServiceContract

    [#|2014-10-07T13:03:50.348+0200|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=34;_ThreadName=Thread-1;|2014-10-07 13:03:50,348 [http-thread-pool-9048-(19)] ERROR pojo.BasicLazyInitializer  - Javassist Enhancement failed: com.itf.propart.DienstleistungsVertragTM
java.lang.NoSuchMethodError: javassist.util.proxy.ProxyFactory.setUseCache(Z)V
    at com.sun.enterprise.web.WebModule.contextListenerStart(WebModule.java:535)
    at com.sun.enterprise.web.WebModule.start(WebModule.java:499)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1947)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1619)
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:90)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:126)
    at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:241)
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:236)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:339)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
    at org.glassfish.admingui.common.util.LocalDeploymentFacility$LocalDFCommandRunner.run(LocalDeploymentFacility.java:138)
    at org.glassfish.deployment.client.AbstractDeploymentFacility.deploy(AbstractDeploymentFacility.java:350)
    at org.glassfish.admingui.common.util.DeployUtil.invokeDeploymentFacility(DeployUtil.java:89)
    at org.glassfish.admingui.common.util.DeployUtil.deploy(DeployUtil.java:66)
    at org.glassfish.admingui.common.handlers.DeploymentHandler.deploy(DeploymentHandler.java:186)
    at com.sun.jsftemplating.layout.descriptors.handler.Handler.invoke(Handler.java:442)
    at com.sun.jsftemplating.layout.descriptors.LayoutElementBase.dispatchHandlers(LayoutElementBase.java:420)
    at com.sun.jsftemplating.layout.descriptors.LayoutElementBase.dispatchHandlers(LayoutElementBase.java:394)
    at com.sun.jsftemplating.layout.event.CommandActionListener.invokeCommandHandlers(CommandActionListener.java:150)
    at com.sun.jsftemplating.layout.event.CommandActionListener.processAction(CommandActionListener.java:98)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:772)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at com.sun.webui.jsf.component.WebuiCommand.broadcast(WebuiCommand.java:166)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at com.sun.webui.jsf.util.UploadFilter.doFilter(UploadFilter.java:223)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:239)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:-1)

我的问题中描述的问题只是another issue在部署时发生的一个后果。