@Query ์ฌ์ฉ
@Query๋ฅผ ์ฌ์ฉํด์ Repository์์ ์ฟผ๋ฆฌ๋ฌธ(DML)์ ์ํํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด๋ณด์.
์ด๋ ธํ ์ด์
@Modifying
JPA๊ฐ ์ง์ํ๋ Select ๊ตฌ๋ฌธ ์ธ์ DML(insert, update, delete)๋ฅผ ์ํํ๊ธฐ ์ํด์ @Modifying
์ด๋
ธํ
์ด์
์ด ํ์ํ๋ค.
@Query(value = "์ฟผ๋ฆฌ๋ฌธ", nativeQuery = true)
@Query
๋ฅผ ์ ์ธํ ๋ค, value์ ์ฌ์ฉํ ์ฟผ๋ฆฌ๋ฌธ์ ์
๋ ฅํ๊ณ , nativeQuery
๋ฅผ true๋ก ์ค์ ํด์ผ ํ๋ค.
์์
FeedbackRepository
import com.vividswan.studymate.model.Feedback;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface FeedbackRepository extends JpaRepository<Feedback, Long> {
@Modifying
@Query(value = "INSERT INTO feedback(userId, taskId, content, createDate) VALUES(?1, ?2, ?3, now())",nativeQuery = true)
void createReply(long userId, long taskId, String content);
}
์ด๋
ธํ
์ด์
์ ์ ์ธํ๊ณ , value์ ์ํ๋ ์ฟผ๋ฆฌ๋ฌธ์ ๋ฃ์ด์คฌ๋ค.
Insert ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ด ๋ง๋ค ์ ์๋ค.
์ด๋ ? + ๋ฉ์๋ ์ธ์ ๋ฒํธ
๋ก ์ฟผ๋ฆฌ๋ฌธ ์๋ฆฌ์ ๋ค์ด๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ์ ์ธํ ์ ์๋ค.
์์ ์์ ์์
- ?1 => long userId
- ?2 => long taskId
- ?3 => String content
๋ก ๋ง์ถฐ์ ์ฟผ๋ฆฌ๋ฌธ์ ๊ฐ์ด ๋ค์ด๊ฐ๋ค.
ํ ์คํธ
๊ฐ์ ๋ฃ์ด๋ณด๊ณ ์ฝ์ ์ฐฝ์ ํ์ธํด๋ณด์.
์์ฑํ๋ ์ฟผ๋ฆฌ๋ฌธ์ด ์ ์ฉ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
'JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
N+1 ๋ฌธ์ ์ ํด๊ฒฐ๋ฐฉ๋ฒ (0) | 2022.03.17 |
---|---|
JPA MappedSuperclass๋ก Entity ์์ ํด๋์ค ์์ฑ (0) | 2022.03.14 |
JPA์์ Pageable๋ก ํ์ด์ง ์ฒ๋ฆฌํ๊ธฐ (0) | 2022.03.13 |
ORM(Object Relational Mapping) (0) | 2022.03.13 |
JPA ์ํฐํฐ ์ด๋ ธํ ์ด์ (0) | 2022.03.12 |