๐ DB ์ํ
1๏ธโฃ Question ํ ์ด๋ธ
@Getter
@Setter
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY
private Integer id;
@Column(length = 200)
private String subject;
@Column(columnDefinition = "TEXT")
private String content;
private LocalDateTime createDate;
}
2๏ธโฃ Answer ํ ์ด๋ธ
@Getter
@Setter
@Entity
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column (columnDefinition = "TEXT")
private String content;
private LocalDateTime createDate;
@ManyToOne // question_id ๋ฅผ FK ๋ก ํ๋ ForeignKey ๊ด๊ณ ์์ฑ
private Question question;
}
Question ๊ณผ Answer ์ 1 : N (-> OneToMany) ๊ด๊ณ์ฑ์ ๊ฐ์ง๊ณ ์๋ค.
๐ ๋ฌธ์ ์ํฉ
์ด ๋, MySQL ์ฝ์์์ Truncate table; ์ฟผ๋ฆฌ๋ฅผ ์คํ์ํค๋ฉด ์๋์ ๊ฐ์ ์ธ๋ํค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
Cannot truncate a table referenced in a foreign key constraint (`qna`.`answer`, CONSTRAINT `FK8frr4bcabmmeyyu60qt7iiblo` FOREIGN KEY (`question_id`) REFERENCES `qna`.`question` (`id`))
์์ธ์ ์๋์ ๊ฐ์ด ์ธ๋ํค์ ์ํด ์ ์ฝ ์ฌํญ์ด ์๊ฒผ๊ธฐ ๋๋ฌธ์ด๋ค.

์์ ๋์ผํ ์ํฉ์ Test ๋ฅผ ์์ฑํ์ฌ ์คํํด๋ณด์๋ ๋ง์ฐฌ๊ฐ์ง ์ด๋ค.
- QuestionRepository
public interface QuestionRepository extends JpaRepository<Question, Integer> {
@Transactional
@Modifying
// @Query ์ ์ํด ์์ฑ๋ ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ด ์ผ์ด๋๋ ์ฝ์
, ์์ , ์ญ์ ์ฟผ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋ ํ์
@Query(value = "truncate question", nativeQuery = true)
void truncate();
}
- Test ์์ฑ
@Test
void testJpa() {
Question q1 = new Question();
q1.setSubject("์ ๋ชฉ");
q1.setContent("๋ด์ฉ");
q1.setCreateDate(LocalDateTime.now());
questionRepository.save(q1);
questionRepository.truncate(); // Truncate ์คํ
}

๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ฐฉ๋ฒ 1๏ธโฃ : ์ธ๋ํค๋ฅผ ์์ค๋ค.
Answer Entity์ ์๋์ ๊ฐ์ด ์ค์ ์ ์ถ๊ฐํด์ค๋ค.
public class Answer {
// .
// .
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) // ์ถ๊ฐ
private Question question;
}
์๋ ์ฌ์ง์ ํตํด์ ์ธ๋ํค ๊ด๋ จ ์ ์ฝ ์ฌํญ์ด ์ฌ๋ผ์ก์์ ์ ์ ์๋ค.

Test ๋ฅผ ์คํํด๋ณด๋ฉด ์ ์์ ์ผ๋ก ์๋ํ๋ค. ํ์ง๋ง ์ธ๋ํค๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐ์ดํฐ ๊ด๋ จ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ ์ ์๋ ๋ฐ๋์ ํ์ํ ์ญํ ์ด๋ค.
๋ฐฉ๋ฒ 2๏ธโฃ : FOREIGN_KEY_CHECKS ํด์ (๊ถ์ฅ)
์ผ์์ ์ผ๋ก ์ธ๋ํค๋ฅผ ํด์ ํ๋ ๋ฐฉ์์ด๋ค. ์๋์ ๊ฐ์ ์ฟผ๋ฆฌ๋ฌธ ์์ฑ์ ํตํด ๊ฐ๋ฅํ๋ค.
SET FOREIGN_KEY_CHECKS = 0; // ๋นํ์ฑํ
TRUNCATE question;
SET FOREIGN_KEY_CHECKS = 1; // ํ์ฑํ
์์ ๊ฐ์ ๊ณผ์ ์ ๋๊ฐ์ด ์งํํด์ฃผ๋ฉด ๋๋ค.
- QuestionRepository
public interface QuestionRepository extends JpaRepository<Question, Integer> {
@Transactional
@Modifying
@Query(value = "truncate question", nativeQuery = true)
void truncate();
// ์ถ๊ฐ (1) : ๋นํ์ฑํ
@Transactional
@Modifying
@Query(value = "SET FOREIGN_KEY_CHECKS = 0", nativeQuery = true)
void disableForeignKeyChecks();
// ์ถ๊ฐ (2) : ํ์ฑํ
@Transactional
@Modifying
@Query(value = "SET FOREIGN_KEY_CHECKS = 1", nativeQuery = true)
void enableForeignKeyChecks();
}
- Test
@Test
void testJpa() {
Question q1 = new Question();
q1.setSubject("์ ๋ชฉ");
q1.setContent("๋ด์ฉ");
q1.setCreateDate(LocalDateTime.now());
questionRepository.save(q1);
questionRepository.disableForeignKeyChecks(); // ๋นํ์ฑํ
questionRepository.truncate(); // Truncate ์คํ
questionRepository.enableForeignKeyChecks(); // ํ์ฑํ
}
์ ์์ ์ผ๋ก ํ ์คํธ๊ฐ ์ํ๋จ์ ์ ์ ์๋ค.