πβοΈ QNA μλΉμ€ κΈ°λ₯ ꡬν (볡μ΅) _ 34κ° ~ 53κ°
1οΈβ£ Entity
β Entity μμ±
- @Getter, @Setter, @Entity μ΄λ Έν μ΄μ νμ
- @Id, @GeneratedValue, @Column λ±μΌλ‘ μ μ½ μ¬ν μΆκ° κ°λ₯
@Getter
@Setter
@Entity // μλ Question ν΄λμ€λ μν°ν° ν΄λμ€μ΄λ€.
// μλ ν΄λμ€μ 1:1λ‘ λ§€μΉλλ ν
μ΄λΈμ΄ DBμ μλ€λ©΄, μλμΌλ‘ μμ±λμ΄μΌ νλ€.
public class Question { // Question μν°ν°
@Id // primary key
@GeneratedValue(strategy = GenerationType.IDENTITY) // auto_increment
private Integer id;
@Column(length = 200) // varchar(200)
private String subject;
@Column(columnDefinition = "TEXT") // μ€κΈ ννμ΄λ―λ‘ TEXT
private String content;
private LocalDateTime createDate;
}
β‘ λ Entity μ κ΄κ³μ± (Question <-> Answer)
- Question ν κ°λΉ μ¬λ¬ κ°μ Answer κ° μμ μ μλ€. Question μ΄ μμΌλ©΄ Answer κ° μ‘΄μ¬ν μ μμΌλ―λ‘ Answer μμ κ΄κ³μ±μ ννν΄ μ£Όμ΄μΌ νλ€. (Answer μν°ν°κ° Question μν°ν°λ₯Ό μ°Έμ‘°νλ νν)
@Getter
@Setter
@Entity
public class Answer {
// .
// .
@ManyToOne // question_id λ₯Ό FK λ‘ νλ ForeignKey κ΄κ³ μμ±
private Question question;
}
Answer κ° Question μ μ°Έμ‘°νκ² λλ©΄ Answer μ question_id 컬λΌμ΄ μΆκ°λκ³ , question_id λ₯Ό FK λ‘ νλ ForeignKey κ΄κ³κ° μμ±λλ€.


μ΄λ, λ§μ½ μ§λ¬Έμ λν λ΅λ³λ€μ μ½κ² κ°μ§κ³ μ€λ €λ©΄ Question μν°ν°μ Answer μμ μμ‘΄μ±μ μΆκ°ν΄μ£Όλ©΄ λλ€. (Question μ΄ Answer λ₯Ό μ°Έμ‘°νλ νν) ν΄λΉ κ³Όμ μ νμμ μΈ κ³Όμ μ μλλ€.
@Getter
@Setter
@Entity
public class Question {
// .
// .
@OneToMany (mappedBy = "question", cascade = CascadeType.REMOVE)
private List<Answer> answerList;
}
mappedBy λ₯Ό ν΅ν΄ Answer μ μ΄λ 컬λΌκ³Ό Mapping λλμ§ νν, cascade λ λ°μ΄ν° λ³νμ λν μ°μμ± μ€μ (ex. νΉμ μ§λ¬Έμ΄ μμ λλ©΄ ν΄λΉ μ§λ¬Έκ³Ό κ΄λ ¨λ answer List λ₯Ό λΆλ¬μμ ν΄λΉ answer λ€λ μμ ν¨)

μ΄ κ³Όμ μμλ Question Entity ꡬ쑰μ λν λ³νλ μλ€.
2οΈβ£ Repository
Entity μ μν΄ μμ±λ DB ν μ΄λΈμ μ κ·Όνλ λ©μλλ€ (findAll, save λ±) μ μ¬μ©νκΈ° μν μΈν°νμ΄μ€. ν μ΄λΈμ λν CRUD κ³Όμ μ μ²λ¦¬νλ€.
β κ° Entity μ Repository λ₯Ό μ°κ²°νμ¬ κ°μ²΄ μμ±, μμ , μμ λ±μ λ³νκ° ν μ΄λΈμ μ μ©λ μ μλλ‘ν¨
import org.springframework.data.jpa.repository.JpaRepository;
public interface QuestionRepository extends JpaRepository<Question, Integer> {
// .
// .
}
- Repository λ μΈν°νμ΄μ€λ‘ JpaRepository λ₯Ό μμλ°λλ€. μ΄λ JpaRepository<(Entity λͺ ), (ν΄λΉ Entity Primary Key μ Type> ννλ‘ λͺ μν΄μ£Όμ΄μΌνλ€.
β‘ μμ
- SQL λ¬Έ
INSERT INTO question
SET createDate = "2022-08-10 13:38:00",
subject = "μ λͺ©",
content = "λ΄μ©";
- μλ° κ°μ²΄ μμ± λ° Repository λ₯Ό ν΅ν μ μ₯
// Question ν
μ΄λΈμ λ°μ΄ν° Insert
Question q1 = new Question();
q1.setSubject("μ λͺ©");
q1.setContent("λ΄μ©");
q1.setCreateDate(LocalDateTime.now());
questionRepository.save(q1); // ν΄λΉ κ³Όμ μμ id λ μλμΌλ‘ μμ±λ¨
μλ° κ°μ²΄λ₯Ό μμ±νκ³ μ μ₯ν¨μΌλ‘μ 볡μ‘νκ³ , μ€ν κ°λ₯μ±μ΄ λ§μ SQL λ¬Έμ λ체ν μ μλ€. μ΄λ₯Ό ORM (Object Relation Mapping) μ΄λΌκ³ νλ€. ν΄λΉ κ³Όμ μ JPA λΌκ³ 보면 λλ€.
3οΈβ£ JPA : μ¬λ¬ find λ©μλ
Query μμ± κ·μΉ : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
| νλͺ© | μμ | μ€λͺ |
| And | findBySubjectAndContent(String subject, String content) | μ¬λ¬ 컬λΌμ and λ‘ κ²μ |
| Or | findBySubjectOrContent(String subject, String content) | μ¬λ¬ 컬λΌμ or λ‘ κ²μ |
| Between | findByIdBetween(Integer id, Integer id) | 컬λΌμ betweenμΌλ‘ κ²μ |
| LessThan | findByIdLessThan(Integer id) | μμ νλͺ© κ²μ |
| GreaterThanEqual | findByIdGraterThanEqual(Integer id) | ν¬κ±°λ κ°μ νλͺ© κ²μ |
| Like | findBySubjectLike(String subject) | like κ²μ |
| In | findBySubjectIn(String[] subjects) | μ¬λ¬ κ°μ€μ νλμΈ νλͺ© κ²μ |
| OrderBy | findBySubjectOrderByCreateDateAsc(String subject) | κ²μ κ²°κ³Όλ₯Ό μ λ ¬νμ¬ μ λ¬ |
λͺ¨λ λ©μλλ SQL λ¬Έκ³Ό Mapping λλ€.
4οΈβ£ Truncate
λ°λ‘ μ 리 : https://like099.tistory.com/24
(JPA) Truncate μ€λ₯ ν΄κ²° (Cannot truncate a table referenced in a foreign key constraint)
π DB μν 1οΈβ£ Question ν μ΄λΈ @Getter @Setter @Entity public class Question { @Id @GeneratedValue(strategy = GenerationType.IDENTITY private Integer id; @Column(length = 200) private String subje..
like099.tistory.com
'TIL π' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| TlL 6μΌμ°¨ (2022.10.14) (0) | 2022.10.14 |
|---|---|
| TIL 5μΌμ°¨ (2022.10.13) (0) | 2022.10.13 |
| TIL 4μΌμ°¨ (2022.10.12) (0) | 2022.10.12 |
| TIL 2μΌμ°¨ (2022.10.07) (0) | 2022.10.07 |
| TIL 1μΌμ°¨ (2022.10.06) (0) | 2022.10.06 |