์ค๋์ ์ค์ ์คํ ์ญ ํ ํ๋ก์ ํธ๋ง ์งํํ์๋ค. ์งํํ๋ฉด์ ๋ฐฐ์ ๊ณ ๊ฒช์๋ ์ ์ ์ ๋ฆฌํ๊ฒ ์ต๋๋ค.
๋ฐฐ๊ฒฝ์ผ๋ก ์ํ ํ
์ด๋ธ, ์ด๋ฏธ์ง ํ
์ด๋ธ์ด ์์ต๋๋ค.
์ํ์ ๋ฑ๋กํ ๋ ์ฌ๋ฌ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ๋ฑ๋กํ ์ ์๋ค๊ณ ๊ฐ์ ํ์ฌ ์ํ ํ
์ด๋ธ๊ณผ ์ด๋ฏธ์ง ํ
์ด๋ธ์ 1 : N ๊ด๊ณ๋ก ์ ์ ํ์์ต๋๋ค.
@Entity
@Getter
@Table(name = "product")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SuperBuilder(toBuilder = true)
public class Product extends BaseEntity {
@OneToMany(mappedBy = "product")
private List<Image> productImages = new ArrayList<>();
// ์ฐ๊ด๊ด๊ณ ๋ฉ์๋
// ์๋น์ค ๋จ์์ createProductImage static ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ
// Image ๊ฐ์ฒด ๋ฐํํ addProductImage ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ Image ๊ฐ์ฒด ์ฃผ์
public void addProductImage(Image productImage) {
productImages.add(productImage);
productImage.setProduct(this);
}
public static Image createProductImage(String imageUrl) {
return new Image(imageUrl);
}
}
Product ์ํฐํฐ ํด๋์ค์ addProductImage ๋ฉ์๋์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
log ์ ๋๋ฒ๊น
์ ํตํด ํ๋ผ๋ฏธํฐ๋ก ๊ฐ์ด ๋์ด ์ค๋์ง ํ์ธํ์ง๋ง,
์ ์์ ์ผ๋ก ๊ฐ์ด ๋์ด์์ผ๋ฉฐ, productImages, List ๊ฐ์ฒด๊ฐ null ์ด๋ผ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
์ฆ List<Image> ๊ฐ ์ด๊ธฐํ๊ฐ ์๋๋ค๋ ๋ป์
๋๋ค.
ํด๋น ๋ถ๋ถ์์ mappedBy ๋งคํ์ด ์๋ชป ๋์๋์ง, ํธ๋์ญ์
์ฒ๋ฆฌ๊ฐ ์๋ชป ๋์๋์ง ๋ง์ ์๊ฐ์ ๊ณ ๋ฏผํ์๋ค....๐ค
โก ๋ง์ด ์กฐ๊ธ ์ด๋ ค์ด๊ฒ ๊ฐ๋ค. ์ฝ๋๋ก ํ์ด๋ณด์๋ฉด, ์๋์ ๊ฐ์ ๊ฒ ๊ฐ๋ค.
@Builder
@AllArgsConstructor
@Getter
public class BuilderTest {
private String test = "exam";
private int num = 1;
private List<Integer> store = new ArrayList<>();
}
public class test1{
public static void main(String[] args) {
BuilderTest a = new BuilderTest();
System.out.println(a.getTest()); // exam ์ถ๋ ฅ
}
}
public class test2{
public static void main(String[] args) {
BuilderTest b = BuilderTest.builder().build();
System.out.println(a.getTest()); // null ๋ฐํ
}
}
@Builder.Default ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ์ด๊ธฐ๊ฐ null ์ด ์๋ ์์ฑํ ArrayList๋ก ์ด๊ธฐํ ํฉ๋๋ค.
@Entity
@Getter
@Table(name = "product")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SuperBuilder(toBuilder = true)
public class Product extends BaseEntity {
@OneToMany(mappedBy = "product")
// ํด๋น ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ๋น๋๋ฅผ ํตํด ์์ฑํ ์ธ์คํด์ค์ ๊ธฐ๋ณธ๊ฐ์ ์ธํ
ํฉ๋๋ค.
@Builder.Default
private List<Image> productImages = new ArrayList<>();
public void addProductImage(Image productImage) {
productImages.add(productImage);
productImage.setProduct(this);
}
public static Image createProductImage(String imageUrl) {
return new Image(imageUrl);
}
}
๊ธฐ์กด์ ์ํ์ ์ด๋ฏธ์ง๋ฅผ ์ฒจ๋ถํด์ ๋ฑ๋กํ๋ ๊ธฐ๋ฅ์๋ ๊ฐ์ ์ ์ด ์์์ต๋๋ค.
MultiPartFile ๋ผ๋ ํ์ผ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ผ๋ฉฐ
์ฌ์ง์ด ์ ์ฅ๋ ํจํค์ง ๊ฒฝ๋ก๋ฅผ yml ํ๊ฒฝ๋ณ์๋ก ์ถ์ถ ํ ์ ์๋ค๋ ์ ์ ์์์ต๋๋ค.
public void feedPostImg(final MultipartFile image){
String imageLocation = "product_images/"; // ์ฌ์ง์ด ์ ์ฅ๋ ํจํค์ง ๊ฒฝ๋ก
String imageName = image.getOriginalFilename();
String imagePath = imageLocation + imageName;
// ์ถ๊ฐ ์ฝ๋ ....
}
@Value() ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ root (๊ธฐ๋ณธ?) yml ๊ฒฝ๋ก์ ๋ง์ถฐ ํ๊ฒฝ๋ณ์๋ฅผ ๋งคํ ํฉ๋๋ค.
@Value("${spring.servlet.multipart.location}") private String productImagePath;
- ์๋์ ๊ฐ์ด root (๊ธฐ๋ณธ)
yml์ ํ๊ฒฝ๋ณ์๋ฅผ ์์ฑํด์ฃผ๊ณ ,secret.ymlํ์ผ์ ๋ง๋ค์ด ํ๊ฒฝ๋ณ์์ ๊ฐ์ ์์ฑํด ์ค๋ค.
โกsecret.yml์ ์ฝ์ด๋๋ฆด๋ ค๋ฉด root (๊ธฐ๋ณธ)yml์ ์๋์ ๊ฐ์ด ์์ฑ ํด์ผsecret.yml์ ์ธ์ ํ ์ ์์ต๋๋ค.spring: profiles: active: include: secret