在 spring jpa 中执行 Hibernate.initialize() 时获取并发修改异常

我正在使用带有 spring data jpa 的 spring boot 应用程序。当我遍历对象时出现并发修改异常。


当我遍历 submission.getPages() 时,我得到了 concurrentModificationException。


我厌倦了检查 null 和 empty 但没有运气


@Override

    public void processCopyAttachmentsToProcessAttachments(int processId) {

        Optional<Process> processOpt = processRepository.findById(processId);

        if (processOpt.isEmpty()) {


            return;

        }

        Process process = processOpt.get();

        Hibernate.initialize(process.getSubmissons());

        List<Submission> submissions = process.getSubmissons();

        if (submissions == null || submissions.isEmpty())

            return;


        for (Submission submission : submissions) {

            Hibernate.initialize(submission.getPages());        

            if (submission.getPages() == null || submission.getPages().isEmpty())

                return;

            for (SubmissionPage submissionPage : submission.getPages()) {           

                Hibernate.initialize(submissionPage.getAttachments());

                if (submissionPage.getAttachments() == null || submissionPage.getAttachments().isEmpty())

                    return;         

                for (SubmissionPageAttachment submissionPageAttachment : submissionPage.getAttachments()) {

                    ProcessAttachment processAttachment = new ProcessAttachment();

                    processAttachment.setDocumentId(submissionPageAttachment.getDocumentId());

                    processAttachment.setDocumentType(submissionPageAttachment.getDocumentType());

                    processAttachment.setProcess(process);

                    processAttachmentRepository.save(processAttachment);

                }

            }

        }

    }

如何解决这个问题?


撒科打诨
浏览 231回答 1
1回答

红颜莎娜

再次检查您的问题,我认为罪魁祸首是Hibernate.initialize. 因为它修改了传递的对象。这是该方法的源代码。正如我们清楚地看到的,它修改了它们的状态,并且因为我们知道ConcurrentModificationException在迭代时内容被更改时会发生这种情况,这显然是原因。&nbsp; &nbsp; public static void initialize(Object proxy) throws HibernateException {&nbsp; &nbsp; &nbsp; &nbsp; if ( proxy == null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( proxy instanceof HibernateProxy ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( (HibernateProxy) proxy ).getHibernateLazyInitializer().initialize();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ( proxy instanceof PersistentCollection ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( (PersistentCollection) proxy ).forceInitialization();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ( proxy instanceof PersistentAttributeInterceptable ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) proxy;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( proxy, null );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }当使用常规的 for 循环时,它一次从集合中访问一个对象,因此它不会出现异常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java