我收到此错误.. org.hibernate.HibernateException:找到相同集合的两个表示

时间:2013-10-09 18:40:41

标签: java spring hibernate

我得到 org.hibernate.HibernateException:在trans.commit()上发现了两个相同集合错误的表示; (第65行)..这可能是什么原因?

public class ImportPublishFiles {
    private WorkspaceService workspaceService = null;
    private DataSource dataSource = null;
    private CompanyService companyService = null;
    private CompanyNameTokenService companyNameTokenService = null;

    public WorkspaceService getWorkspaceService() {
        return workspaceService;
    }

    public void setWorkspaceService(WorkspaceService workspaceService) {
        this.workspaceService = workspaceService;
    }

     public void execute() throws Exception {
        Workspace workspace = getWorkspaceService().findWorkspaceByVendorAndStatus(Workspace.SOURCE_INFOUSA, Workspace.STATUS_REQUEST_SUBMITTED_PUBLISH);
        if( workspace != null && workspace.getStatus().equals(Workspace.STATUS_REQUEST_SUBMITTED_PUBLISH)){
            try{
                Transaction trans = getCompanyService().beginTransaction();
                String workspaceId = workspace.getWorkspaceID();
                workspace.setStatus(Workspace.STATUS_EXECUTING_PUBLISH);
                System.out.println("After changing to executing publish   ");
                getWorkspaceService().save(workspace);
                trans.commit();

我收到上述内容中的错误

                /*if (!trans.wasCommitted())
                {
                    System.out.println("Before Commit   : ");
                  trans.commit();
                }*/
                // do the publish here
                publish( workspace, "admin" );
                // Force it to be reloaded
                getWorkspaceService().evict(workspace);
                trans = getCompanyService().beginTransaction();
                workspace.setStatus( Workspace.STATUS_PUBLISHED );
                workspace.setPublishedDate( new Date() );
                getWorkspaceService().merge( workspace );
                trans.commit();
                /*   if (!trans.wasCommitted())
                {
                  trans.commit();
                }*/

            }catch(Exception e){
                log.error(e.toString());
                System.out.println("After Commit status  : " + workspace.getStatus());
                  System.out.println(e.toString() );
                e.printStackTrace();

            }
        }
    }

    private void publish( Workspace workspace, String userID ) throws Exception
    {
        // Step 1: Get list of matching/verification results
        ArrayList alRecords = getWorkspaceService().getWorkspaceMatchResults( workspace );

        Connection con = getDataSource().getConnection();
        Company c = null;
        MatchResult mr = null;
        StatusReport sr = new StatusReport();
        sr.setCountTotalRecords( alRecords.size() );
        writeStatus( workspace, sr );
        int countProcessed = 0;

        try
        {
            // Step 2: For each record
            for ( int i = 0; i < alRecords.size(); i++ )
            {
                Transaction trans = getCompanyService().beginTransaction();
                mr = (MatchResult)alRecords.get( i );
                String resultCode = mr.getResultCode();     // result of the matching process
                boolean bProcessed = false;

                if ( CompanyWrapper.MATCH_DATAUPDATE.equals( resultCode ) )     // recognize from previous import
                {
                    c = getWorkspaceService().parseCompanyFromImport( "" + mr.getUploadDataCk() );
                    updateCompanyProfile( mr.getMatchSelection(), c, userID );
                    sr.addCountUpdated(1);
                    bProcessed = true;
                }
                else if ( CompanyWrapper.MATCH_NONE.equals( resultCode ) )      // new profile
                {
                    c = getWorkspaceService().parseCompanyFromImport( "" + mr.getUploadDataCk() );
                    createCompanyProfile( c, userID );
                    sr.addCountCreated(1);
                    bProcessed = true;
                }
                else if ( CompanyWrapper.MATCH_WEAK.equals( resultCode ) ||     // possibly matched 
                          CompanyWrapper.MATCH_STRONG.equals( resultCode ) )    //   against profile
                {
                    String matchedCompanyID = mr.getMatchSelection();

                    if ( ( matchedCompanyID != null ) && 
                         ( matchedCompanyID.length() > 0 ) )                    // Yup!  do something
                    {
                        c = getWorkspaceService().parseCompanyFromImport( "" + mr.getUploadDataCk() );

                        if ( "_".equals( matchedCompanyID ) )                   // user denied ANY match
                        {
                            createCompanyProfile( c, userID );
                            sr.addCountCreated(1);
                        }
                        else
                        {
                            updateCompanyProfile( matchedCompanyID, c, userID ); // add data to company profile
                            sr.addCountMatched(1);
                        }
                        bProcessed = true;
                    }
                }
                else if ( CompanyWrapper.MATCH_UNIQUE.equals( resultCode ) )    // matched against profile 
                {
                    c = getWorkspaceService().parseCompanyFromImport( "" + mr.getUploadDataCk() );
                    updateCompanyProfile( mr.getMatchSelection(), c, userID );
                    sr.addCountMatched(1);
                    bProcessed = true;
                }
                else if ( CompanyWrapper.MATCH_PROCESSED.equals( resultCode ) ) // already been processed 
                {
                    ;    // don't do anything
                    sr.addCountPreviouslyProcessed(1);
                }
                else    // we don't know what to do with it
                {
                    log.warn( "Don't know how to publish result code = '" + resultCode + "' for DATA_UPLOAD_CK = " + mr.getUploadDataCk() );

                      System.out.println( "Don't know how to publish result code = '" + resultCode + "' for DATA_UPLOAD_CK = " + mr.getUploadDataCk() );
                    sr.addCountUnprocessed(1);
                }

                sr.addCountProcessed(1);  // note one more record processed

                if ( bProcessed )
                {
                    mr.setResultCode( CompanyWrapper.MATCH_PROCESSED );
                    getWorkspaceService().setWorkspaceMatchResult( mr , con );

                    if ( ++countProcessed % 100 == 0 )      // housekeeping
                    {
                        getCompanyService().flushAndClear();
                        workspace = writeStatus( workspace, sr ); // update stats
                    }
                }
                  if (!trans.wasCommitted())
                    {
                        trans.commit();
                    }

            }

            sr.close();
            workspace = writeStatus( workspace, sr );
        }
        catch ( Exception ex )
        {
            log.error( "Exception caught on saving data from CK : " + ( mr == null ? "null" : "" + mr.getUploadDataCk() ) + " : " + ex.toString() );

              System.out.println( "Exception caught on saving data from CK : " + ( mr == null ? "null" : "" + mr.getUploadDataCk() ) + " : " + ex.toString()  );
            if ( c != null )
                log.error( c.getFieldStringsForDebug( ) );
              System.out.println( c.getFieldStringsForDebug( ) );
            throw ex;
        }
        finally
        {
            if ( con != null )
            {
                try { con.close(); } catch ( Exception ex ) {} con = null;
            }
        }
    }

    // NOTE:  To insure Hibernate can write this, the evict() MUST be called before getting here!
    private Workspace writeStatus( Workspace workspace, StatusReport sr )
    {
        String workspaceId = workspace.getWorkspaceID();
        getWorkspaceService().evict( workspace );           // remove from cache
        workspace = (Workspace)getWorkspaceService().findById( workspaceId );
        workspace.getWorkspaceVariables().put( "publish.status.0.currentStatus", 
                                                new WorkspaceVariables( workspace, "publish.status.0.currentStatus", sr.getCurrentStatus() ) );

        workspace.getWorkspaceVariables().put( "publish.status.1.countTotalRecords", 
                new WorkspaceVariables( workspace, "publish.status.1.countTotalRecords", "" + sr.getCountTotalRecords() ) );

        workspace.getWorkspaceVariables().put( "publish.status.2.countProcessed", 
                new WorkspaceVariables( workspace, "publish.status.2.countProcessed", "" + sr.getCountProcessed() ) );

        workspace.getWorkspaceVariables().put( "publish.status.3.percentProcessed", 
                new WorkspaceVariables( workspace, "publish.status.3.percentProcessed", "" + sr.calcPercentProcessed() ) );

        workspace.getWorkspaceVariables().put( "publish.status.4.countUpdated", 
                new WorkspaceVariables( workspace, "publish.status.4.countUpdated", "" + sr.getCountUpdated() ) );

        workspace.getWorkspaceVariables().put( "publish.status.5.countCreated", 
                new WorkspaceVariables( workspace, "publish.status.5.countCreated", "" + sr.getCountCreated() ) );

        workspace.getWorkspaceVariables().put( "publish.status.6.countMatched", 
                new WorkspaceVariables( workspace, "publish.status.6.countMatched", "" + sr.getCountMatched() ) );

        workspace.getWorkspaceVariables().put( "publish.status.7.countPreviouslyProcessed", 
                new WorkspaceVariables( workspace, "publish.status.7.countPreviouslyProcessed", "" + sr.getCountPreviouslyProcessed() ) );

        workspace.getWorkspaceVariables().put( "publish.status.8.countUnprocessed", 
                new WorkspaceVariables( workspace, "publish.status.8.countUnprocessed", "" + sr.getCountUnprocessed() ) );

        workspace.getWorkspaceVariables().put( "publish.status.9.elapsedTime", 
                new WorkspaceVariables( workspace, "publish.status.9.elapsedTime", "" + sr.getElapsedTime() ) );

        getWorkspaceService().evictAndSave(workspace);
        return ( workspace );
    }
    private void updateCompanyProfile( String profileID, Company c, String userID ) throws Exception 
    {
        Toolbox tb = new Toolbox( userID );
        Integer profileId = new Integer( profileID );
        c.setCompanyProfileId( profileId );
        tb.setProfileRecords( getCompanyService().findByCompanyProfileId( profileId ) );
        tb.updateProfileRecord( c );

        if ( tb.mergeProfileRecords() ) // something has changed, need to save it   
        {
            ArrayList al = tb.getDirtyRecords();
            for ( int i = 0; i < al.size(); i++ )
                getCompanyService().save( (Company)al.get( i ) );

            if ( tb.nameTokenUpdateRequired() )
            {
                Company viewCompany = tb.findCurrentViewRecord();

                if ( viewCompany != null )
                {
                    CompanyWrapper cw = new CompanyWrapper( viewCompany );
                    ArrayList tokens = cw.getNameTokens();
                    getCompanyNameTokenService().saveCompanyNameTokens( viewCompany.getCompanyProfileId(), tokens );
                }
            }
        }
    }

    private void createCompanyProfile( Company c, String userID ) throws Exception
    {
        Integer newID = getCompanyService().allocateProfileId();

        // save the vendor record
        c.setCompanySeqNum( 0 );
        c.setCompanyProfileId( newID );
        c.applyAuditInformation( userID );
        getCompanyService().save( c );          // write out InfoUSA data

        // create the "view" record
        Company view = (Company)c.cloneCompany();
        view.setDataSourceCode( Company.SOURCE_VIEWRECORD );                // indicate this is the viewable record
        view.setCompanySeqNum(0);
        view.setSourceRecordId( "" );               // view records have no import source
        view.applyAuditInformation( userID );
        getCompanyService().save( view );           // write out View record

        // now save the name tokens for future matching
        CompanyWrapper cw = new CompanyWrapper( c );
        getCompanyNameTokenService().saveCompanyNameTokens( newID, cw.getNameTokens() );
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public CompanyService getCompanyService() {
        return companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    public CompanyNameTokenService getCompanyNameTokenService() {
        return companyNameTokenService;
    }

    public void setCompanyNameTokenService(
            CompanyNameTokenService companyNameTokenService) {
        this.companyNameTokenService = companyNameTokenService;
    }

}

0 个答案:

没有答案