์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
29 | 30 |
- bineary search
- ๋๋น์ฐ์ ํ์
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ํ์ด์ฌ ์ค๋ฅ
- ํ๋ก๊ทธ๋๋จธ์ค
- rest api
- ๊น์ด์ฐ์ ํ์
- ์ง๋ํ์ต
- BOJ
- ๋ฐฑ์ค
- ๊ฐํํ์ต
- ์ฝ๋ฉํ ์คํธ
- ์บ์ฑ
- ์๋ฐ
- HTTP
- ์ฝํ
- ๋จธ์ ๋ฌ๋
- ํด์
- ์ค๋ฒ๋ผ์ด๋ฉ
- Merge sort
- ๋น์ง๋ํ์ต
- ์ฝ๋ฉ
- ์ด์งํ์
- ์๊ณ ๋ฆฌ์ฆ
- ์คํ๊ณผ ํ
- ๋์ ๋๋ฆฌ
- ๋ฉฑ๋ฑ
- post
- ํ์ด์ฌ
- ๋ฅ๋ฌ๋
- Today
- Total
chae._.chae
[Error] : For queries with named parameters you need to use provide names for method parameters ๋ณธ๋ฌธ
[Error] : For queries with named parameters you need to use provide names for method parameters
walbe0528 2022. 2. 22. 10:56๐ฏ ์คํ๋ง JPA์์ nativeQuery๋ฅผ ์ด์ฉํ์ฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ ๋ ๋ฐ์ํ ์๋ฌ
์๋์ ํ ์คํธ ์ฝ๋๋ฅผ ์คํ์์ผฐ๋๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
@DataJpaTest // JPA์ ์ฐ๋ํ ํ
์คํธ
class CommentRepositoryTest {
@Autowired
CommentRepository commentRepository;
@Test
@DisplayName("ํน์ ๊ฒ์๊ธ์ ๋ชจ๋ ๋๊ธ ์กฐํ") // ํ
์คํธ ๊ฒฐ๊ณผ์ ๋ณด์ฌ์ค ์ด๋ฆ
void findByArticleId() {
/* Case 1 : 4๋ฒ ๊ฒ์๊ธ์ ๋ชจ๋ ๋๊ธ ์กฐํ*/
{
// ์
๋ ฅ๋ฐ์ดํฐ ์ค๋น
Long articleId = 4L;
// ์ค์ ์ํ
List<Comment> comments = commentRepository.findByArticleId(articleId);
// ์์ํ๊ธฐ
Article article = new Article(4L, "๋น์ ์ ์ธ์ ์ํ๋?", "๋๊ธ ใฑ");
Comment a = new Comment(1L, article, "Park", "๊ตณ ์ ํํ
");
Comment b = new Comment(2L, article, "Kim", "์์ด ์ ์");
Comment c = new Comment(3L, article, "Choi", "์ผ์ํฌ์ ํ์ถ");
List<Comment> expected = Arrays.asList(a, b, c);
// ๊ฒ์ฆ
assertEquals(expected.toString(), comments.toString(), "4๋ฒ ๊ธ์ ๋ชจ๋ ๋๊ธ ์ถ๋ ฅ!");
}
}
}
- CommentRepository ์ธํฐํ์ด์ค
public interface CommentRepository extends JpaRepository<Comment, Long> {
// ํน์ ๊ฒ์๊ธ์ ๋ชจ๋ ๋๊ธ ์กฐํ
@Query(value =
"SELECT *" +
" FROM comment" +
" WHERE article_id = :articleId",
nativeQuery = true)
List<Comment> findByArticleId(Long articleId);
// ํน์ ๋๋ค์์ ๋ชจ๋ ๋๊ธ ์กฐํ
List<Comment> findByNickname(String nickname);
}
- findBynickname์ xmlํ์ผ
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
http://xmlns.jcp.org/xml/ns/persistence/orm_2_2.xsd"
version="2.2">
<named-native-query
name="Comment.findByNickname"
result-class="com.example.firstproject.entity.Comment">
<query>
<![CDATA[
SELECT
*
FROM
comment
WHERE
nickname = :nickname
]]>
</query>
</named-native-query>
</entity-mappings>
[Error ๋ฌธ๊ตฌ]
org.springframework.dao.InvalidDataAccessApiUsageException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.; nested exception is java.lang.IllegalStateException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.
[๋ฌธ์ ํด๊ฒฐ]
@Param ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๊ฑฐ๋, Java8+์์๋ -parameters๋ฅผ ์ฌ์ฉํ๋ผ๊ณ ์๋ ค์ฃผ๊ณ ์๋ค.
SQL ๋ฌธ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ ๋๋ ์ ๋ฌ๋๋ ๋ณ์๋ค์ @Param์ด๋ ธํ ์ด์ ์ ๋ถ์ฌ์ค์ผํ๋ค.
์ ์ด์ฃผ์ง ์์ผ๋ฉด ํ๋ผ๋ฏธํฐ๋ฅผ ์ฐพ์ง ๋ชปํด ์๋ฌ๊ฐ ๋๋ค. (@Param๋ฟ๋ง ์๋๋ผ, '#{ }'๋ฅผ ์ฌ์ฉ๋๊ธฐ๋ ํ๋ค.)
@Query(value =
"SELECT *" +
" FROM comment" +
" WHERE article_id = :articleId",
nativeQuery = true)
List<Comment> findByArticleId(@Param("articleId")Long articleId);
์์ ์ฝ๋์ฒ๋ผ, @Param("articleId")๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ํด๊ฒฐ๋๋ค
+ ์ถ๊ฐ
findByArticleId๋ง๊ณ findBynickname์๋ ์๋ ์ฝ๋์ฒ๋ผ @Param์ ๋ฃ์ด์ค์ผํ๋ค....
(findnickname ํ ์คํธ์์๋ ์๋ฌ๊ฐ ๋ฐ์ํด์ ์์ ์๊ฒ,,)
์๋ฌ๊ฐ ๋ฐ์ํ๋๊ฒ ์ง๊ทนํ ๋น์ฐํ์ง๋ง ๊ทธ๋๋ ์์ง์ ์ข ๋ฌด์์..
List<Comment> findByNickname(@Param("nickname")String nickname);
๋ ์๋ฌ ๋ชจ๋ @Param ์ด๋ ธํ ์ด์ ์ ์ ์ง ์์์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฐพ์ง ๋ชปํ๋ ์๋ฌ์ธ๋ฐ, ์๋ฌ ๋ฌธ๊ตฌ๊ฐ ์กฐ๊ธ ๋ฌ๋๋ค.
findBynickname์ ์๋ฌ๋ฌธ๊ตฌ๋ ๋ค์๊ณผ ๊ฐ๋ค.
[Error ๋ฌธ๊ตฌ]
org.springframework.dao.InvalidDataAccessApiUsageException: Could not locate ordinal parameter [1], expecting one of []; nested exception is java.lang.IllegalArgumentException: Could not locate ordinal parameter [1], expecting one of []
๋ด ์๊ฐ์ผ๋ก๋, findByArticleId๋ @Query์ด๋ ธํ ์ด์ ์ ํตํด์ ์ง์ sql์ ์์ฑํ์๊ณ (native query๋ฅผ ์ฌ์ฉํ์๊ณ ), findBynickname์ sql์ xml๋ก ์์ฑํ์ฌ ๋ค๋ฅธ ๊ฒ ๊ฐ๋ค.
(์ ํํ์ง์์..)