๋ณตํฉ ํค(Composite Key) ์ฌ๋ฌ ์ด์ ์กฐํฉํ์ฌ ๊ธฐ๋ณธ ํค(Primary Key)์ ์ญํ ์ ํ ์ ์๋๋ก ๋ง๋ ํค๋ฅผ ์๋ฏธํฉ๋๋ค. ์๋ณ ๊ด๊ณ ๋ถ๋ชจ ํ
์ด๋ธ์ ๊ธฐ๋ณธ ํค๋ฅผ ๋ด๋ ค๋ฐ์์ ์์ ํ
์ด๋ธ์ ๊ธฐ๋ณธ ํค + ์ธ๋ ํค๋ก ์ฌ์ฉํ๋ ๊ด๊ณ์
๋๋ค. ๋น์๋ณ ๊ด๊ณ ๋ถ๋ชจ ํ
์ด๋ธ์ ๊ธฐ๋ณธ ํค๋ฅผ ์์ ํ
์ด๋ธ์ ์ธ๋ ํค๋ก๋ง ์ฌ์ฉํ๋ ๊ด๊ณ์
๋๋ค. ๋น์๋ณ ๊ด๊ณ๋ ํ์์ ๋น์๋ณ ๊ด๊ณ์ ์ ํ์ ๋น์๋ณ ๊ด๊ณ๊ฐ ์์ต๋๋ค. ํ์์ ๋น์๋ณ ๊ด๊ณ(Mandatory) ์ธ๋ ํค์ NULL์ ํ์ฉํ์ง ์์ต๋๋ค. ์ฐ๊ด๊ด๊ณ๋ฅผ ๋ฐ๋์ ๋งบ์ด์ผ ํฉ๋๋ค. ์ ํ์ ๋น์๋ณ ๊ด๊ณ(Optional) ์ธ๋ ํค์ NULL์ ํ์ฉํฉ๋๋ค. ์ฐ๊ด๊ด๊ณ๋ฅผ ๋งบ์์ง ๋ง์ง ์ ํํ ์ ์์ต๋๋ค. ์๋ณ ๊ด๊ณ์ ๋น์๋ณ ๊ด๊ณ์ ์ ํ ์ต๊ทผ์๋ ๋น์๋ณ ๊ด๊ณ๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๊ณ ๊ผญ ํ์ํ ๊ณณ์๋ง ..
๋ฑ๋ก๋ ๋ชจ๋ ๋น ์กฐํํ๊ธฐ ๋ฑ๋ก๋ ์คํ๋ง ๋น์ ํ์ธํ๊ธฐ ์ํด ApplicationContext๋ฅผ ์ฃผ์
๋ฐ์ ๋ค, beanDefinitionNames()๋ฅผ ํตํด ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฐ๋จํ ์์๋ฅผ ์ํด CommandLineRunner๋ฅผ ํตํด ๋น์ด ๋ชจ๋ ๋ฑ๋ก๋ ํ ๋น์ ์ถ๋ ฅํ๋๋ก ๋ง๋ค์์ต๋๋ค. @Component class BeanPrinter( ac: ApplicationContext ) : CommandLineRunner { private final val ac: ApplicationContext = ac override fun run(vararg args: String?) { val beanDefinitionNames = ac.beanDefinitionNames for (beanDefini..
์ ๋ ์ง๊ธ๊น์ง ์ผ๋ฐ์ ์ผ๋ก JPA๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ @Id์ @GenerateValue, ๊ทธ๋ฆฌ๊ณ Long ํ์
์ id๋ฅผ ์ฌ์ฉํ์์ต๋๋ค. ํญ์ id๋ ์ง์ ํ ๋นํ์ง ์์๊ธฐ์ save()์ id์ ๊ฐ์ ํญ์ null์ด์์ต๋๋ค. ๊ทธ๋ฌ๋ค ์ฝํ๋ฆฐ์ ๊ณต๋ถํ๊ณ , ์ฝํ๋ฆฐ์ผ๋ก JPA๋ฅผ ์ฌ์ฉํด๋ณด๋ ค๋ ์ค, ์ฝํ๋ฆฐ์์ id์ null์ ํ์ฉํ๊ฒ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค๋ฉด ์ฝ๋๊ฐ ๋๋ฌด ๋๋ฌ์์ง ๊ฒ ๊ฐ์์, null์ ์ฌ์ฉํ์ง ์๋ ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด์์ต๋๋ค. ์๊ฐ๋ณด๋ค ๊ฐ๋จํ๋๋ฐ ๊ฒฐ๋ก ์ 0์ผ๋ก ์ด๊ธฐํ ์์ผ์ฃผ๋ฉด ๋๋ค์์ต๋๋ค. ํน์ nullable ํ์
์ ์ฌ์ฉํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์
๋๋ค. ์ด์ ๋ถํฐ ์ด๋ฌํ ์ด์ ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. save()๋ ์ด๋ป๊ฒ ์๋ํ ๊น? ์ ์ฅํ๋ ค๋ ์ํฐํฐ๊ฐ ์๋ก์ด ์ํฐํฐ์ธ ๊ฒฝ์ฐ persist๋ฅผ, ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ merge๋ฅผ ์ง..
๊ฒฐ๋ก ๋ถํฐ ๋ง์๋๋ฆฌ๋ฉด JSON ํ์์ผ๋ก ๋ฐ์ดํฐ๊ฐ ๋ฐํ๋ฉ๋๋ค. ์์๋ฅผ ์ํด ๋ค์ ์ฝ๋๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. @Controller public class MemberController { @GetMapping("/") public MemberDto getInfo(){ return new MemberDto("์ ๋ํ",12)); } } ํด๋น ์ฝ๋๋ฅผ ์คํํ๋ฉด ๋น์ฐํ๊ฒ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด๋ฌํ ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ @Controller๋ฅผ @RestController๋ก ๋ฐ๊พธ์ด์ฃผ๊ฑฐ๋, @ResponseBody๋ฅผ ๋ถ์ฌ์ฃผ์ด์ผ ํ ๊ฒ์
๋๋ค. ๋๋ ResponseEntity๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค. ResponseEntity๋ฅผ ์ฌ์ฉํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. @Controller public class MemberContr..
@WebMvcTest๋ฅผ ํตํด Controller์ ๋ํ ํ
์คํธ๋ฅผ ์งํํ๋ ๊ฒฝ์ฐ Spring Security๋ฅผ ์ ์ฉํ์๋ค๋ฉด Security๊ฐ ์ ๊ณตํ๋ ๊ธฐ๋ณธ์ ์ธ Filter๋ค์ ๋ํ์ฌ ์ด๋ป๊ฒ ๋์ํด์ผ ํ๋์ง ๊ฑฑ์ ์ด ๋ ์๋ ์์ผ๋ฉฐ, ํ
์คํธ ์งํ ๋์ค Error creating bean~~๋ฑ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์๋ ์์ต๋๋ค. ์ด์ ๋ํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค. ์ด๋ค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋์? ๐ง ์๋๋ ์ ๊ฐ ๋ฐ์์์ผฐ๋ ์ค๋ฅ์ด๋ฉฐ, ์ ๊ฐ ์์ฑํ ํด๋์ค์ ์ด๋ฆ์ด๋ Directory์ ๋ํ ๊ฒฝ๋ก๋ ~~ ๋ก ์ฒ๋ฆฌํ์์ต๋๋ค. Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '~~..
@RequestBody๋ฅผ ์ฌ์ฉํ๋ฉด MappingJackson2HttpMessageConverter๊ฐ ObjectMapper๋ฅผ ํตํด ๊ฐ์ ๋ฐ์ธ๋ฉํด์ค๋๋ค. ์ฆ @RequestBody์ ๋ฐ์ธ๋ฉ ์กฐ๊ฑด์ ObjectMapper์ ๋ฐ์ธ๋ฉ ์กฐ๊ฑด๊ณผ ๋์ผํฉ๋๋ค. ํด๋น ๊ณผ์ ์ ๋งจ ๋ง์ง๋ง์ ์์ธํ ์ดํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์ฐ์ ๊ธฐ๋ณธ์ ์ธ ์ํฉ์์ ๋ฐ์ธ๋ฉ์ด ๋๋ ์กฐ๊ฑด์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๊ฐ๋ฅํ ์กฐ๊ฑด (๋ชจ๋ ์ ๊ทผ์ ์ด์) ๊ธฐ๋ณธ์์ฑ์ + (public) Getter ๊ฐ๋ฅ (๋ชจ๋ ์ ๊ทผ์ ์ด์) ๊ธฐ๋ณธ์์ฑ์ + (๋ชจ๋ ์ ๊ทผ์ ์ด์) Setter ๊ฐ๋ฅ ๋ถ๊ฐ๋ฅํ ์กฐ๊ฑด ๊ธฐ๋ณธ์์ฑ์๋ง ์๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์์ฑ์๊ฐ ์๋ ๊ฒฝ์ฐ (๋ชจ๋ ์ ๊ทผ์ ์ด์) ๊ธฐ๋ณธ์์ฑ์ + (public์ด ์๋ ๋ค๋ฅธ ์ ๊ทผ์ ์ด์) Getter ObjectMapper ์กฐ๊ฑด ๋ณ๊ฒฝํ๊ธฐ object..
์ํฉ 1 - String ํ์
์ List์ธ ๊ฒฝ์ฐ "๋ฆฌ์คํธ๋ช
" : [ "์์1", "์์2", "์์3", "์์4" ] ์์์ " "๋ ์๋ต๊ฐ๋ฅ ์์ public class Blog { private String address; private List visitor = new ArrayList(); public List getVisitor() { return visitor; } public String getAddress() { return address; } public Blog() { } } ํ
์คํธ์ฝ๋ @SpringBootTest class ObjectMapperTest { private ObjectMapper objectMapper = new ObjectMapper(); private static ..
๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ํ์ง๋ ์์ ์ ์์ง๋ง, ์ ์ ๊ฒฝ์ฐ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ dialect(๋ฐฉ์ธ)์ด ๋ฌธ์ ์์ต๋๋ค. ์ application.properties ์์๋ ๋ค์๊ณผ ๊ฐ์ด dialect๊ฐ ์ค์ ๋์ด ์์๊ณ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect ์ด๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๊ณ ์ณ์ฃผ๋ ์ค๋ฅ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect ์ฐธ๊ณ - MySQL ์คํ ๋ฆฌ์ง ์์ง ์ข
๋ฅ์ ํน์ง https://nomadlee.com/mysql-%EC%8A%A4%ED%86%A0%EB%A6%AC%EC%A7%80-%EC%97%94%EC%..