[Project] 전자결재 구현, JdbcSQLIntegrityConstraintViolationException 예외

Updated:

Categories:

Tags: ,

📌 개인적인 공간으로 공부를 기록하고 복습하기 위해 사용하는 블로그입니다.
정확하지 않은 정보가 있을 수 있으니 참고바랍니다 :😸
[틀린 내용은 댓글로 남겨주시면 복받으실거에요]

이번 프로젝트에서 DocumentWorkflow 간의 관계를 처리하는 방식에 대해 고민이 많았다.

흐름을 어떻게 설계 해야 할지 너무 복잡했기 때문에 우선 approval과 workflow는 따로 구현하다가

하나의 흐름으로 가는게 맞다고 생각되어 합쳤다.

그래서 Document도 그렇게 하려고 했는데 template랑 template_field는 하나의 서비스에서 함께 구현하고 type은 전체적인 template를 관리하는 구조로 갔다 이걸 OneToOne으로 구현 할지 ManyToOne으로 변경할지 고민했는데 너무 복잡도가 올라 갈 것 같고 예전에 일할때는 연차 낼 때 하나의 양식밖에 없었던 것 같아서 OneToOne으로 구현햇다.

제일 고민은 Document를 어떻게 구현하냐는 것이었는데

  1. 결재라인을 생성해야하므로 Workflow와 apporval을 미리 생성하고
  2. Template와 type을 생성 후
  3. Document 생성시에 type과 workflow를 함께 처리해주는게 맞다고 생각했다.

DocumentService에서 Workflow와 함께 처리

DocumentWorkflow@OneToOne 관계로 설정되어 있기 때문에, 문서를 생성할 때 워크플로우도 같이 생성되도록 처리했다.

1
2
3
4
5
6
7
8
9
10
11
{
    "documentTypeId" :1,
    "workflowId" :1,
    "title" : "연차 신청 건",
    "content": "휴가 가요",
    "author" : "제리",
    "customFields": {
        "연차시작일": "2024-12-01",
        "연차종료일": "2024-12-02"
    }
}

Mapper


미리 만들어진 workflow와 documentType은 mapper에서 연결해주었다,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 default Document postDtoToDocument(DocumentDto.Post postDto) {
        
        //DocumentType 생성
        DocumentType documentType = DocumentType.builder()
                        .id(postDto.getDocumentTypeId()).build();
        
        //Workflow 생성
        Workflow workflow = new Workflow();
        workflow.setId(postDto.getWorkflowId());
        
        Document.DocumentBuilder document = Document.builder();
        
        ////DocumentType 저장
        document.documentType(documentType);
        
        //Workflow 저장
        document.workflow(workflow);
        
        document.title(postDto.getTitle());
        document.content(postDto.getContent());
        document.author(postDto.getAuthor());
        return document.build();
    }

Service


그리고 mapper에서 변환된 document 가 들어왔을 때 id가 들어왔기 때문에 각각의 서비스를 DI받은 다음 호출하여 id를 각각 검증해주었다

1
2
3
4
5
6
7
8
9
public Document createDocument(Document document) {

        //workflow id, documentType id 검증
        DocumentType documentType = docsTypeService.findDocsType(document.getDocumentType().getId());
        Workflow workflow = workflowService.findWorkflow(document.getWorkflow().getId());
        document.setDocumentType(documentType);
        document.setWorkflow(workflow);
        return repository.save(document);
    }

JdbcSQLIntegrityConstraintViolationException

1. 문제 발생

  • 다 만들고 나서 이제 Test 하는데 template 생성시 오류 발생…!

  • JdbcSQLIntegrityConstraintViolationException 에 대한 오류

    • Template Post할 때 원래 TemplateId가 들어가지도 않고, field에도 templateId가 없는데 왜 저런 에러가 생겼는거지? 하고 해당 부분으로 이동했다.

2. 원인 확인

  • DocsTemplateService

    • 딱히 구현한 내용이 없고 이름만 검증한 다음에 저장하기 때문에 문제가 없는데 Save에서 문제가 생긴거면 DB에 들어갈 때 문제가 생기는 것 같았다.
  • 디버깅 포인트 잡고 실행

    • repository에 저장할 때 Template와 TemplateField 간에 연관관계가 설정되어있는데 id가 없어서 문제가 생긴 것 같다.
      • DocsTemplate Entity

    • repository 저장할 때 확인해보니 template랑 field id가 모두 null 이다…

3. 해결방법

  • 아무래도mapper에서 기본적으로 해주는 것은 field와 DocumentTemplate를 연결을 못해주는 것 같아서 default 로 변경하여 직접 설정해주었다.

4. 해결완료

  • mapper 수정 후 Test 진행했더니 201 Created 로 응답이왔다.!!!

  • 혹시 몰라서 DB도 확인해봤는데 문제없음!


전자결재 하나 구현하는 것도 참 오래걸리고 복잡도가 높은 것 같다…ㅠㅠ







Project 카테고리 내 다른 글 보러가기

Leave a comment