์ฒ์์๋ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๋ค์ค ํ ์ฝ์ ์ ๊ตฌํํ์๋ค.
์ค๋ผํด์ INSERT ALL๊ณผ MyBatis์ foreach๋ฅผ ํผํฉ ์ฌ์ฉํ์ฌ INSERT ALL ์ดํ์ INTO ~ VALUES๋ฅผ ๋ฐ๋ณตํ๋ ๊ฒ์ด๋ค.
์ด ํ ์ด๋ธ์ REPORT_MEMBER ์ํ์ค ๊ฐ์ ๊ฐ์ ธ์ PK๋ก ์ฌ์ฉํ๋๋ฐ,
์ด์ฒ๋ผ INSERT ์ ์ํ์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ ์ผ๋ฐ์ ์ธ BULK INSERT๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
(ํ๋์ ํ์ผ๋ก ์ธ์ํ์ฌ ์ํ์ค๋ฅผ ํ ๋ฒ๋ง ํธ์ถํจ. ๋ฌด๊ฒฐ์ฑ ์ค๋ฅ ๋ฐ์)
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด(INTERN_TASK.PK_REPORT_MEMBER)์ ์๋ฐฐ๋ฉ๋๋ค
INSERT ALL ์ดํ์ INTO VALUES๋ง ๋ฐ๋ณต์์ผ์ ๊ทธ๋ฐ๊ฐ ํ๊ณ
INSERT INTO VALUES๋ก ๋จ์ผํ์ ์ฌ๋ฌ ๋ฒ ์คํํด๋ณด๊ธฐ๋ ํ๋๋ฐ
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL ๋ช
๋ น์ด๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ข
๋ฃ๋์ง ์์์ต๋๋ค
์ค๋ผํด์์๋ ํ ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ INSERT ์คํ์ด ๋ถ๊ฐํ๋ค๊ณ ํจ ...
์ฌ๋ฌ ๊ฐ์ INSERT INTO VALUES๋ฅผ ํ๋์ ์ฟผ๋ฆฌ๋ก ์ธ์ํด์ ์๋ชป๋ ์ฟผ๋ฆฌ ์ค๋ฅ๊ฐ ๋๋ ์ํฉ
์ฒ์์ ๊ตฌ๋ถ์ ์ค๋ฅ์ธ์ค ์๊ณ ์ฝค๋ง๋ ๋ถ์ฌ๋ณด๊ณ ๊ณต๋ฐฑ๋ ๋์๋ณด๊ณ ์ธ๋ฏธ์ฝ๋ก ๋ ๋ถ์ฌ๋ณด๊ณ ์์น๋ ์ฎ๊ฒจ๋ณด๊ณ ๋๋ฆฌ๋ฅผ ์ณค๋๋ฐ ์๋๋๋ผ
๋๋ฌธ์ ์๋์ ๊ฐ์ด UNION ALL์ ์ฌ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝํ์๊ณ , ์ฟผ๋ฆฌ ๋์๊น์ง ํ์ธํ์๋ค.
<!-- ๋ด๋ถ SELECT์์ foreach ์ฌ์ฉ → ๊ฐ memberId๋ฅผ UNION ALL๋ก ๋ณํฉ
๊ฐ UNION ALL ๊ฒฐ๊ณผ์ ๋ํด ๊ฐ๋ณ์ ์ผ๋ก REPORT_MEMBER_SEQ.NEXTVAL ํธ์ถ (Sequence๊ฐ PK) -->
<insert id="saveReportMember">
INSERT INTO INTERN_TASK.REPORT_MEMBER (ID, REPORT_ID, MEMBER_ID, CREATED_AT, CREATED_BY)
SELECT REPORT_MEMBER_SEQ.NEXTVAL, reportId, memberId, SYSDATE, createdBy
FROM (
<foreach collection="reportMembers" item="member" open="(" separator=" UNION ALL " close=")">
SELECT #{member.reportId} AS reportId, #{member.memberId} AS memberId, #{member.createdBy}
AS createdBy FROM DUAL
</foreach>
)
</insert>
INSERT ALL์ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ์๋ ์ธก๋ฉด์์๋ ๋น ๋ฅด๋ค๋ ๋ฒค์น๋งํน ์๋ฃ๋ฅผ ๋ดค๋๋ฐ, ํ์ฌ ์ ๋ฌด์์ ํ๋์ SQL ์ฟผ๋ฆฌ๊ฐ ๊ธธ์ด์ง๋ฉด ์ด์ ํ๊ฒฝ์์์ ๋ชจ๋ํฐ๋ง์ด ํ๋ค๋ค๊ณ ... ๋จ์ผ ํ์ ์ฌ๋ฌ ๋ฒ ํธ์ถํ๋ ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝํ๋ผ๊ณ ์ง์ ๋ฐ์
๋ ์ ๊ฑฐ ํด๊ฒฐ๋ฐฉ๋ฒ ์ฐพ๋๋ผ ๊ฑฐ์ ์ดํ ์ฝ์งํ๋๋ฐ ๋ ์ฝ์ง์ ํด
๊ฒฐ๊ตญ ๋น์ฆ๋์ค ๋ก์ง์์ ๋จ์ผ INSERT๋ฌธ ์ฌ๋ฌ ๋ฒ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ฐ๊พธ๊ธฐ๋ก ํจ
// 2. REPORT_MEMBER
if (reportMemberReqDTO.getMemberIds() != null && !reportMemberReqDTO.getMemberIds().isEmpty()) {
List<ReportMember> reportMemberList = reportMemberReqDTO.toDomains(reportId, createMemberId);
for (ReportMember reportMember : reportMemberList) {
reportCommandOutPort.saveReportMember(reportMember); // ๊ฐ๋ณ Insert ์คํ
}
}
<insert id="saveReportMember">
INSERT INTO INTERN_TASK.REPORT_MEMBER (ID, REPORT_ID, MEMBER_ID, CREATED_AT, CREATED_BY)
VALUES (REPORT_MEMBER_SEQ.NEXTVAL, #{reportId}, #{memberId}, SYSDATE, #{createdBy})
</insert>