์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- docker
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- nGrinder
- Kafka
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- aws saa ํฉ๊ฒฉ
- s3 log ์ ์ฅ
- private subnet ec2 ๋ก์ปฌ ์ ์
- redis ํ ์คํธ์ฝ๋
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- ์คํํ๋ ๋ฏธ์ค
- redis ์กฐํ
- ํ๋ก๊ทธ๋๋จธ์ค
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- prod docker-compose
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- ์๋ฐ
- ๋ค์ค ์ปจํ ์ด๋
- Entity
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- docker ps -a
- docker-compose kafka
- docker compose
- ํ์ดํผ๋ฐ์ด์
- Codedeploy ์ค๋ฅ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- aws ์ฟ ํฐ
- JPA
- AWS Certified Solutions Architect - Associate
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Spring] Jpa๋ฅผ ์ด์ฉํ ์กฐํ์ ์ฆ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ ๋ณธ๋ฌธ
[Spring] Jpa๋ฅผ ์ด์ฉํ ์กฐํ์ ์ฆ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
๐คRyusun๐ค 2023. 1. 27. 02:32ํ๋ก์ ํธ์์ Jpa๋ฅผ ํ์ฉํด์ ์กฐํ์ ์ฆ๊ฐ ๊ธฐ๋ฅ์ ๊ตฌํํด ๋ณด์์ต๋๋ค.
BoardController
/**
* ์กฐํ์ ์ฆ๊ฐ
*/
@ApiOperation(value = "์กฐํ์ ์ฆ๊ฐ", notes = "์กฐํ์๋ฅผ ์ฆ๊ฐํฉ๋๋ค.")
@GetMapping("/board/hit/{id}")
public int updateHit(@PathVariable("id") final Long id) {
return boardService.updateHit(id);
}
BoardService
/**
* ์กฐํ์ ์ฆ๊ฐํ๊ธฐ
*/
@Transactional
public int updateHit(Long id) {
Board board = boardRepository.findById(id).orElseThrow(IllegalArgumentException::new);
return boardRepository.updateHit(board.getId());
}
BoardRepository
Test์ ์งํํ๋ฉด์ ์๋ฌ๊ฐ ๊ณ์ ๋ฌ๋ ๋ถ๋ถ์ด repository์์ต๋๋ค.
์์ ์
public interface BoardRepository extends JpaRepository<Board, Long> {
@Modifying
@Query("update Board set hit = :hit + 1 where id = :id")
int updateHit(@Param("id") Long id );
}
์ค๋ฅ 1. org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query
ํด๊ฒฐ๋ฒ : @Transactional ์ service์ ์จ์ repository์๋ ์ ์จ๋ ๋๋ ์ค ์์์ง๋ง ํ ์คํธ์ฝ๋์์๋ rollback์ ์ํด @Transactional์ ์ฌ์ฉํด์ผ ํ๋ค๊ณ ํฉ๋๋ค. ๋จ์ํ JpaRepository์ ๊ธฐ๋ฅ์ ํ ์คํธํ๊ธฐ ์ํด์๋ผ๋ฉด @DataJpaTest๋ฅผ ์ด์ฉํ๋ฉด @Transactional์ rollback ๊ธฐ๋ฅ์ ๋์ ํฉ๋๋ค.
- ๊ฐ๋จํ๊ฒ @Transactional์ ์ค๋ช ํ๋ฉด @Transactional์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ณ ์ญ์ ๋ฅผ ํ๋ ๋์ค ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ๋ค์ ๋กค๋ฐฑ๋๋๋ก ํ๋ ์ด๋ ธํ ์ด์ ์ ๋๋ค.
์ค๋ฅ 2. org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter not bound : hit; ~
jpa update๋ฌธ์ ์ฌ๋ฌ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ๋ฉด์ ์์ ๊ฐ์ด ๊ตฌํํด ๋ณด์์ง๋ง test ์ฝ๋์์ ์์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฌ์์ต๋๋ค.
ํด๊ฒฐ๋ฒ : hit ์์ : ๋๋ฌธ์ jpa๊ฐ param๋ก ์๊ฐํด์ ์ค๋ฅ๊ฐ ๋๋ ๊ฒ์ด์์ต๋๋ค. ๋ฐ๋ผ์ : ์ ์ง์ฐ๊ณ ์คํํ์๋๋ ์ค๋ฅ ์์ด ์ ์๋ํ์์ต๋๋ค.
์์ ํ
public interface BoardRepository extends JpaRepository<Board, Long> {
@Transactional
@Modifying
@Query("update Board set hit = hit + 1 where id = :id")
int updateHit(@Param("id") Long id );
}
ํ ์คํธ ํต๊ณผ
[์ฐธ๊ณ ]
https://tecoble.techcourse.co.kr/post/2020-08-31-jpa-transaction-test/
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐ฉ๐ซ๐ข๐ง๐ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Spock (0) | 2023.05.15 |
---|---|
[Spring] Docker (0) | 2023.05.14 |
[Spring] Entity,์์์ฑ ์ปจํ ์คํธ (1) | 2022.12.31 |
[Spring] DI (์ ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด์ผ ํ๋๊ฐ?) (0) | 2022.12.27 |
[Spring] JPA, ORM (0) | 2022.12.20 |