[FIX] 품목 수정시 품목코드 에러 수정#44
Conversation
Walkthrough부품 코드 생성 로직이 최신 항목 기반에서 대상 그룹의 모든 부품을 스캔하여 최대 시퀀스 번호를 찾는 방식으로 변경되었습니다. 새로운 코드는 최대 시퀀스에 1을 더하여 계산되며, 나머지 흐름은 변경되지 않았습니다. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/main/java/com/sampoom/backend/api/part/service/PartService.java(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (java-kotlin)
| Page<Part> parts = partRepository.findByPartGroupId(groupId, PageRequest.of(0, Integer.MAX_VALUE)); | ||
|
|
||
| int maxSeq = 0; | ||
| for (Part part : parts.getContent()) { | ||
| if (part.getCode() != null && part.getCode().startsWith(codePrefix)) { | ||
| String[] codeParts = part.getCode().split("-"); | ||
| if (codeParts.length == 3) { | ||
| try { | ||
| int seq = Integer.parseInt(codeParts[2]); | ||
| maxSeq = Math.max(maxSeq, seq); | ||
| } catch (NumberFormatException ignored) { | ||
| // 잘못된 형식의 코드는 무시 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return String.format("%s-%s-%03d", categoryCode, groupCode, nextSeq); | ||
| int nextSeq = maxSeq + 1; | ||
| String newCode = String.format("%s-%s-%03d", categoryCode, groupCode, nextSeq); | ||
|
|
||
| log.debug("그룹 {} 코드 생성: 현재 최대 시퀀스={}, 새 코드={}", groupId, maxSeq, newCode); |
There was a problem hiding this comment.
Integer.MAX_VALUE 페이지 요청이 런타임 예외를 유발합니다
PageRequest.of(0, Integer.MAX_VALUE)로 모든 부품을 로딩하면 Hibernate 5.3+에서 내부적으로 페이지 크기에 +1을 더해 slice 여부를 판단하는 과정에서 int 오버플로가 발생해 IllegalArgumentException: first-result value cannot be negative가 즉시 던져집니다. 실제로 동일한 패턴을 사용한 코드가 운영 중에 바로 해당 예외로 실패한 사례가 있습니다.(stackoverflow.com) 또한 전체 파트를 한 번에 적재하게 되므로, Spring Data가 기본으로 최대 페이지 크기를 2 000으로 제한하는 이유와 같이 대용량 데이터를 한 번에 읽지 말아야 한다는 가이드라인도 위배됩니다.(jackzhang.cn) 이 로직은 결국 코드 재생성 시 500 에러를 유발할 수 있으므로 반드시 수정이 필요합니다.
권장 수정 방향은 DB가 최댓값을 계산하도록 위임하는 것입니다. 예를 들어 PartRepository에 Optional<String> findFirstByPartGroupIdAndCodeStartingWithOrderByCodeDesc(Long groupId, String prefix); 같은 메서드를 추가해 단 1건만 조회한 뒤, 해당 코드의 시퀀스 부분만 파싱해서 nextSeq를 계산하면 됩니다. 또는 JPQL/SQL로 MAX를 직접 조회해 int로 받아오는 전용 쿼리를 두는 것도 가능합니다. 핵심은 전 테이블 스캔과 Integer.MAX_VALUE 페이징을 제거해 예외와 성능 문제를 동시에 해소하는 것입니다.
🤖 Prompt for AI Agents
In src/main/java/com/sampoom/backend/api/part/service/PartService.java around
lines 369 to 389, the current PageRequest.of(0, Integer.MAX_VALUE) causes int
overflow and loads all parts; replace this approach by delegating the
max-sequence lookup to the database: add a repository method that returns either
the single highest code row (e.g.
findFirstByPartGroupIdAndCodeStartingWithOrderByCodeDesc) or a query that
selects MAX(seq) directly, call that method to obtain the latest code (or max
sequence), parse its sequence part to compute nextSeq, and remove the large page
request and full-table scan to prevent the overflow and performance issue.
📝 Summary
🙏 Question & PR point
📬 Reference
Summary by CodeRabbit
릴리스 노트