保存或更新问题

时间:2009-08-13 14:57:21

标签: java hibernate jboss ejb-3.0

我们有一个应用程序,它在服务器(jboss.4.2.1.GA)端有一个swing客户端和java应用程序。我们正在使用ejb3。 在我们的应用程序中,我们在某些时候成功创建了一张发票并将其显示给用户,但是当用户想要查询发票时,我们会收到以下错误。重新加载发票后,用户可以更改发票而不会出现任何错误。 发票创建代码,发票更改和完整错误堆栈如下:

public Invoice createInvoice(String invoiceNo, CreateInvoiceGroupTemplate template, Collection serviceCalculations)
        throws Exception
{
    manager.setFlushMode(FlushModeType.COMMIT);

    if (glDao.isInvoiceNoInUse(manager, invoiceNo))
        throw new NonUniqueInvoiceNoException(invoiceNo);

    Invoice invoice = new Invoice();
    invoice.setNo(invoiceNo);
    invoice.setInvoiceDate(template.getMinimumInvoiceDate());
    Date paymentDay = findInvoicePaymentDay(template);
    invoice.setPaymentDate(paymentDay);
    invoice.setFormBeginDate(template.getFormBeginDate());
    invoice.setFormEndDate(template.getFormEndDate());

    Currency currency = new Currency();
    currency.setId(template.getCurrencyId());
    invoice.setCurrency((Currency) dao.findByPrimaryKey(manager, currency));

    Customer customer = new Customer();
    customer.setId(template.getCustomerId());
    invoice.setCustomer((Customer) dao.findByPrimaryKey(manager, customer));

    if (template.getRepresentativeId() > 0)
    {
        Representative representative = new Representative();
        representative.setId(template.getRepresentativeId());
        invoice.setRepresentative((Representative) dao.findByPrimaryKey(manager, representative));
    }

    if (template.getAccountingGroupId() != 0)
    {
        AccountingGroup accountingGroup = new AccountingGroup();
        accountingGroup.setId(template.getAccountingGroupId());
        invoice.setAccountingGroup((AccountingGroup) dao.findByPrimaryKey(manager, accountingGroup));
    }

    if (template.getAirlineGroupId() > 0)
    {
        AirlineGroup airlineGroup = new AirlineGroup();
        airlineGroup.setId(template.getAirlineGroupId());
        invoice.setAirlineGroup((AirlineGroup) dao.findByPrimaryKey(manager, airlineGroup));
    }

    if (template.getAirportId() != 0)
    {
        Airport airport = new Airport();
        airport.setId(template.getAirportId());
        invoice.setAirport((Airport) dao.findByPrimaryKey(manager, airport));
    }

    //automatically create new address based on the last invoice for this customer and representative
    InvoiceAddress oldInvoiceAddress = glDao.findAddressForLastInvoice(manager, invoice);
    if (oldInvoiceAddress != null)
    {
        InvoiceAddress invoiceAddress = (InvoiceAddress) oldInvoiceAddress.copyEntity();
        invoice.setInvoiceAddress(invoiceAddress);
    }

    HashMap invoiceDetails = new HashMap();
    String key = "";

    try
    {
        Collection mappings = invoice.getInvoiceMappings();
        InvoiceMapping invoiceMapping = null;
        ServiceCalculation serviceCalculation = null;
        ServiceCalculation refreshedServiceCalculation = null;
        Iterator itr = serviceCalculations.iterator();
        while (itr.hasNext())
        {
            serviceCalculation = (ServiceCalculation) itr.next();
            invoiceMapping = new InvoiceMapping();

            refreshedServiceCalculation = (ServiceCalculation) dao.findByPrimaryKey(manager, serviceCalculation);
            refreshedServiceCalculation.setInvoiced(true);
            refreshedServiceCalculation.setVatRateModified(serviceCalculation.isVatRateModified());

            if (refreshedServiceCalculation instanceof CalculatedService)
                invoiceMapping.setCalculatedService((CalculatedService) refreshedServiceCalculation);
            else if (refreshedServiceCalculation instanceof CalculatedRoyalty)
                invoiceMapping.setCalculatedRoyalty((CalculatedRoyalty) refreshedServiceCalculation);
            else if (refreshedServiceCalculation instanceof CalculatedCommission)
                invoiceMapping.setCalculatedCommission((CalculatedCommission) refreshedServiceCalculation);

            mappings.add(invoiceMapping);

            serviceCalculation = (ServiceCalculation) dao.saveOrUpdateEntity(manager, refreshedServiceCalculation);

            key = createKey(serviceCalculation);
            processInvoiceDetail(invoiceDetails, key, serviceCalculation);
        }

        ArrayList processedInvoiceDetails = clearInvoiceDetails(invoiceDetails.values());
        invoice.getInvoiceDetails().addAll(processedInvoiceDetails);

        updateExchangeRate(invoice);

        invoice = (Invoice) dao.saveOrUpdateEntity(manager, invoice);

        glDao.initializeInvoice(invoice);
    }
    catch (ApplicationException exc)
    {
        logger.error(exc);
        ctx.setRollbackOnly();
        throw exc;
    }
    catch (Exception exc)
    {
        logger.error(exc);
        ctx.setRollbackOnly();
        throw exc;
    }

    return invoice;
}

public ServerResponse synchronizeInvoice(GridData gridData) throws Exception
{
    ServerResponse response = new ServerResponse();

    try
    {
        manager.setFlushMode(FlushModeType.COMMIT);

        Invoice loadedInvoice = null;

        //boolean invoiceRemoved=false;

        Collection entitiesToRemove = gridData.getGarbageData();
        Iterator itr = entitiesToRemove.iterator();
        Object temp = null;
        Invoice invoice = null;
        InvoiceDetail invoiceDetail = null;
        while (itr.hasNext())
        {
            temp = itr.next();

            if (temp instanceof Invoice)
            {
                invoice = (Invoice) temp;
                loadedInvoice = (Invoice) dao.findByPrimaryKey(manager, invoice);
                if (loadedInvoice.getStatus() == Invoice.INVOICE_FINALIZED
                        || loadedInvoice.getStatus() == Invoice.INVOICE_CANCELLED)
                    throw new InvoiceFinalizedException();
                else
                {
                    updateMappingsAsNotInvoiced(invoice);
                    dao.removeEntity(manager, invoice);
                }
            }
            else
            {
                //instance of invoice detail
                invoiceDetail = (InvoiceDetail) temp;
                dao.removeEntity(manager, invoiceDetail);
            }
        }

        Collection updatedEntity = gridData.getNewUpdatedBuffer();
        updatedEntity.addAll(gridData.getUpdatedBuffer());

        itr = updatedEntity.iterator();
        if (itr.hasNext())
        {
            temp = itr.next();
            invoice = (Invoice) temp;
            loadedInvoice = (Invoice) dao.findByPrimaryKey(manager, invoice);
            if(loadedInvoice!=null)
            {
                if ((loadedInvoice.getStatus() == Invoice.INVOICE_FINALIZED && invoice.getStatus() != Invoice.INVOICE_CANCELLED)
                        || loadedInvoice.getStatus() == Invoice.INVOICE_CANCELLED)
                    throw new InvoiceFinalizedException();
                else
                {
                    if (invoice.getStatus() == Invoice.INVOICE_FINALIZED)
                    {
                        JMSHelper helper = new JMSHelper();
                        helper.sendMessage("queue/InvoiceFinalizeEvent", invoice);
                        finalizeInvoice(invoice);
                    }
                    else if (invoice.getStatus() == Invoice.INVOICE_CANCELLED)
                    {
                        JMSHelper helper = new JMSHelper();
                        helper.sendMessage("queue/InvoiceFinalizeEvent", invoice);
                        cancelInvoice(invoice);
                    }
                    else
                    {
                        updateExchangeRate(invoice);
                        invoice=(Invoice) dao.saveOrUpdateEntity(manager, invoice);
                    }

                    response.addData(invoice);
                }
            }               
        }
        else
        {
            Iterator synchronizedData=gridData.getSynchBuffer().iterator();
            if(synchronizedData.hasNext())
            {
                invoice = (Invoice) synchronizedData.next();
                response.addData(invoice);
            }
        }
    }
    catch (ApplicationException exc)
    {
        ctx.setRollbackOnly();
        response.addException(exc);
        return response;
    }
    catch (Exception exc)
    {
        ctx.setRollbackOnly();
        throw exc;
    }

    return response;
}

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Lorg/hibernate/type/AbstractComponentType;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2291)
at java.lang.Class.getDeclaredField(Class.java:1880)
at java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1610)
at java.io.ObjectStreamClass.access$700(ObjectStreamClass.java:52)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:425)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:413)
at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:310)
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:547)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at java.util.ArrayList.readObject(ArrayList.java:593)
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at java.util.ArrayList.readObject(ArrayList.java:593)
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at org.jboss.aop.joinpoint.InvocationResponse.readExternal(InvocationResponse.java:122)
at java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1792)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.receiveObjectVersion2_2(JavaSerializationManager.java:239)
at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.receiveObject(JavaSerializationManager.java:133)
at org.jboss.remoting.marshal.serializable.SerializableUnMarshaller.read(SerializableUnMarshaller.java:120)
at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.versionedRead(MicroSocketClientInvoker.java:943)
at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.transport(MicroSocketClientInvoker.java:584)
at org.jboss.remoting.MicroRemoteClientInvoker.invoke(MicroRemoteClientInvoker.java:122)
at org.jboss.remoting.Client.invoke(Client.java:1550)
at org.jboss.remoting.Client.invoke(Client.java:530)
at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:61)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:53)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:72)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:103)
at $Proxy5.synchronizeInvoice(Unknown Source)
at com.celebi.ikarus.gl.bdo.GLBusinessDelegator.synchronizeInvoice(GLBusinessDelegator.java:116)
at com.celebi.ikarus.gl.window.WInvoice.saveButtonPressed(WInvoice.java:303)
at com.celebi.ikarus.main.component.toolbar.MainToolBar$5.actionPerformed(MainToolBar.java:129)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

4 个答案:

答案 0 :(得分:2)

您不应该将Hibernate创建的类直接序列化到您的客户端,因为您正在获取发送的Hibernate工件。这是导致NoClassDefFound错误的原因。

您应该使用自己创建的值对象并将Hibernate结果转换为这些对象。这将使您无法访问Hibernate特定类的隐藏链接。

答案 1 :(得分:2)

您使用fetch = FetchType.LAZY定义了一个或多个实体关系。 作为此关系一部分的关联实体将替换为AbstractComponentType。

由于类org.hibernate.type.AbstractComponentType未包含在JBoss客户端jar中,因此JVM抛出了ClassNotFoundException。

因此,要解决此问题,您可以将FetchType更改为EAGER,或者在向客户端发送响应之前,可以确保在会话bean中初始化关系。

(你也可以在你的类路径中添加一个包含AbstractComponentType类的jar,只要客户端不会尝试访问被这个AbstractComponentType替换的实体 - 我不会推荐这最后一个替代。)< / p>

答案 2 :(得分:1)

底层问题是ServerResponse正在填充引用org / hibernate / type / AbstractComponentType的数据,而hibernate类不在swing客户端的类路径上。您可以将hibernate类添加到客户端类路径中,但更有可能的是发回一个比您想象的更重且包含更多引用的对象。

关于该类的信息不足以及您的发票一般如何制作以了解更多信息,但异常表明这是核心问题。

答案 3 :(得分:0)

当您尝试在保存时将Invoice实例发送到服务器时,堆栈跟踪指示客户端中没有类“org.hibernate.type.AbstractComponentType”。

在某个地方定义了一个错误,或者你必须将hibernate JAR添加到客户端的类路径中。