From 4a8f21506912d07b748d47195a3f5f90791c8497 Mon Sep 17 00:00:00 2001 From: JooJooda Date: Sun, 15 Sep 2024 04:26:07 +0900 Subject: [PATCH 1/3] feat : music streaming service design --- README.md | 8 +++++ YoungJu-Lee-Spring/build.gradle | 3 ++ .../YoungJuLeeSpringApplication.java | 2 ++ .../YoungJu_Lee_Spring/domain/Album.java | 31 +++++++++++++++++ .../YoungJu_Lee_Spring/domain/Artist.java | 26 ++++++++++++++ .../domain/BaseTimeEntity.java | 22 ++++++++++++ .../YoungJu_Lee_Spring/domain/Genre.java | 17 ++++++++++ .../YoungJu_Lee_Spring/domain/GenreSong.java | 24 +++++++++++++ .../YoungJu_Lee_Spring/domain/Like.java | 25 ++++++++++++++ .../YoungJu_Lee_Spring/domain/LikeSong.java | 25 ++++++++++++++ .../YoungJu_Lee_Spring/domain/Member.java | 28 +++++++++++++++ .../YoungJu_Lee_Spring/domain/PlayList.java | 27 +++++++++++++++ .../domain/PlayListSong.java | 25 ++++++++++++++ .../YoungJu_Lee_Spring/domain/Song.java | 34 +++++++++++++++++++ .../src/main/resources/application.yml | 10 ++++-- 15 files changed, 304 insertions(+), 3 deletions(-) create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Album.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Artist.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/BaseTimeEntity.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Genre.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/GenreSong.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayListSong.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java diff --git a/README.md b/README.md index 574bdf5..efe4d67 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,11 @@ +
+week2.JPA-standard +
+ + ![스크린샷 2024-09-15 041233](https://github.com/user-attachments/assets/c617af23-e23c-40fe-9f45-3d6f59a5ab96) + +
+
diff --git a/YoungJu-Lee-Spring/build.gradle b/YoungJu-Lee-Spring/build.gradle index a1389d9..a7b06bb 100644 --- a/YoungJu-Lee-Spring/build.gradle +++ b/YoungJu-Lee-Spring/build.gradle @@ -33,6 +33,9 @@ dependencies { // h2 연결 runtimeOnly 'com.h2database:h2' + + // 쿼리 파라미터 확인 + implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' } tasks.named('test') { diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/YoungJuLeeSpringApplication.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/YoungJuLeeSpringApplication.java index 3116cc9..56e676f 100644 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/YoungJuLeeSpringApplication.java +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/YoungJuLeeSpringApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication +@EnableJpaAuditing public class YoungJuLeeSpringApplication { public static void main(String[] args) { diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Album.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Album.java new file mode 100644 index 0000000..0177b98 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Album.java @@ -0,0 +1,31 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; +import java.util.List; +import java.util.ArrayList; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Album { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "album_id") + private Long id; + + private String title; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "artist_id") + private Artist artist; + public void setArtist(Artist artist) { + this.artist = artist; + } + + @OneToMany(mappedBy = "album", cascade = CascadeType.ALL) + private List songs = new ArrayList<>(); + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Artist.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Artist.java new file mode 100644 index 0000000..041974e --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Artist.java @@ -0,0 +1,26 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; +import java.util.List; +import java.util.ArrayList; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Artist { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "artist_id") + private Long id; + private String name; + + @OneToMany(mappedBy = "artist", cascade = CascadeType.ALL) + private List songs = new ArrayList<>(); + + @OneToMany(mappedBy = "artist", cascade = CascadeType.ALL) + private List albums = new ArrayList<>(); + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/BaseTimeEntity.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/BaseTimeEntity.java new file mode 100644 index 0000000..d6bd986 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/BaseTimeEntity.java @@ -0,0 +1,22 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntity { + @CreatedDate + private LocalDateTime createdAt; + + @LastModifiedDate + private LocalDateTime updatedAt; +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Genre.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Genre.java new file mode 100644 index 0000000..08f26a8 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Genre.java @@ -0,0 +1,17 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Genre { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "genre_id") + private Long id; + private String name; +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/GenreSong.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/GenreSong.java new file mode 100644 index 0000000..753c56f --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/GenreSong.java @@ -0,0 +1,24 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +@Table(name = "genre_song") +public class GenreSong { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "genre_id") + private Genre genre; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "song_id") + private Song song; +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java new file mode 100644 index 0000000..7a30d1b --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java @@ -0,0 +1,25 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Like extends BaseTimeEntity{ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "like_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + public void setMember(Member member) { + this.member = member; + } + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java new file mode 100644 index 0000000..3cad082 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java @@ -0,0 +1,25 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +@Table(name = "like_song") +public class LikeSong { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "like_song_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "like_id") + private Like like; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "song_id") + private Song song; +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java new file mode 100644 index 0000000..fcfae16 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java @@ -0,0 +1,28 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; +import java.util.List; +import java.util.ArrayList; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Member extends BaseTimeEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "member_id") + private Long id; + + private String name; + + @Column(nullable = false, unique = true) + private String email; + + @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) + private List playlists = new ArrayList<>(); + + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java new file mode 100644 index 0000000..f54971a --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java @@ -0,0 +1,27 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class PlayList { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "playlist_id") + private Long id; + + private String name; + private String description; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + public void serMember(Member member) { + this.member = member; + } +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayListSong.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayListSong.java new file mode 100644 index 0000000..c883a3f --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayListSong.java @@ -0,0 +1,25 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class PlayListSong extends BaseTimeEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "playlist_song_id") + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "playlist_id") + private PlayList playlist; + + @JoinColumn(name = "song_id") + @ManyToOne(fetch = FetchType.LAZY) + private Song song; + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java new file mode 100644 index 0000000..911e3d5 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java @@ -0,0 +1,34 @@ +package com.example.YoungJu_Lee_Spring.domain; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class Song extends BaseTimeEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "song_id") + private Long id; + + private String title; + + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @JoinColumn(name = "artist_id") + private Artist artist; + + public void setArtist(Artist artist) { + this.artist = artist; + } + + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @JoinColumn(name = "album_id") + private Album album; + + public void setAlbum(Album album) { + this.album = album; + } +} diff --git a/YoungJu-Lee-Spring/src/main/resources/application.yml b/YoungJu-Lee-Spring/src/main/resources/application.yml index b1ff4f7..5123cce 100644 --- a/YoungJu-Lee-Spring/src/main/resources/application.yml +++ b/YoungJu-Lee-Spring/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:h2:~/test-mutsa # 메모리 내 데이터베이스 URL + url: jdbc:h2:tcp://localhost/~/music-program-mutsa # 메모리 내 데이터베이스 URL driver-class-name: org.h2.Driver # H2 드라이버 클래스 username: sa # 기본 사용자 이름 password: # 기본 비밀번호 @@ -8,8 +8,12 @@ spring: console: enabled: true # H2 콘솔을 활성화합니다. path: /h2-console # H2 콘솔의 접근 경로 - hibernate: - ddl-auto: update # Hibernate의 DDL 자동 생성 전략 (update, create, create-drop 등) + jpa: + hibernate: + ddl-auto: create # Hibernate의 DDL 자동 생성 전략 (update, create, create-drop 등) + properties: + hibernate: + format_sql: true sql: init: mode: always # 애플리케이션 시작 시 스크립트 초기화 (기본값: embedded) \ No newline at end of file From e143243dec855861b417a6d06fd1a76966267045 Mon Sep 17 00:00:00 2001 From: JooJooda Date: Sun, 15 Sep 2024 16:24:07 +0900 Subject: [PATCH 2/3] fix : revise Like design --- README.md | 3 ++- .../YoungJu_Lee_Spring/domain/Like.java | 8 +++--- .../YoungJu_Lee_Spring/domain/LikeSong.java | 25 ------------------- .../YoungJu_Lee_Spring/domain/Member.java | 2 ++ .../YoungJu_Lee_Spring/domain/Song.java | 5 ++++ 5 files changed, 13 insertions(+), 30 deletions(-) delete mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java diff --git a/README.md b/README.md index efe4d67..679b53a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ week2.JPA-standard
- ![스크린샷 2024-09-15 041233](https://github.com/user-attachments/assets/c617af23-e23c-40fe-9f45-3d6f59a5ab96) + ![스크린샷 2024-09-15 162049](https://github.com/user-attachments/assets/5a4c23ba-2197-4bb6-8e34-25e169a7b152) +
diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java index 7a30d1b..3809713 100644 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Like.java @@ -8,6 +8,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor @Builder +@Table(name = "likes") public class Like extends BaseTimeEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -18,8 +19,7 @@ public class Like extends BaseTimeEntity{ @JoinColumn(name = "member_id") private Member member; - public void setMember(Member member) { - this.member = member; - } - + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "song_id") + private Song song; } diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java deleted file mode 100644 index 3cad082..0000000 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/LikeSong.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.YoungJu_Lee_Spring.domain; - -import jakarta.persistence.*; -import lombok.*; - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -@Builder -@Table(name = "like_song") -public class LikeSong { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "like_song_id") - private Long id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "like_id") - private Like like; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "song_id") - private Song song; -} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java index fcfae16..ab0e350 100644 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Member.java @@ -24,5 +24,7 @@ public class Member extends BaseTimeEntity { @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) private List playlists = new ArrayList<>(); + @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) + private List likes = new ArrayList<>(); } diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java index 911e3d5..d67d5ed 100644 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/Song.java @@ -2,6 +2,8 @@ import jakarta.persistence.*; import lombok.*; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -31,4 +33,7 @@ public void setArtist(Artist artist) { public void setAlbum(Album album) { this.album = album; } + + @OneToMany(mappedBy = "song", cascade = CascadeType.ALL) + private List likes = new ArrayList<>(); } From 2498390c05ccaaf50dfd7d8726c745034198da4d Mon Sep 17 00:00:00 2001 From: JooJooda Date: Sun, 15 Sep 2024 23:52:17 +0900 Subject: [PATCH 3/3] feat : add repository and test code --- README.md | 29 ++++++++ .../YoungJu_Lee_Spring/domain/PlayList.java | 4 +- .../repository/AlbumRepository.java | 18 +++++ .../repository/ArtistRepository.java | 18 +++++ .../repository/MemberRepository.java | 35 +++++++++ .../repository/PlayListRepository.java | 35 +++++++++ .../repository/PlayListSongRepository.java | 32 ++++++++ .../repository/SongRepository.java | 36 +++++++++ .../src/main/resources/application.yml | 2 +- .../repository/MemberRepositoryTest.java | 46 ++++++++++++ .../repository/PlayListRepositoryTest.java | 55 ++++++++++++++ .../repository/SongRepositoryTest.java | 73 +++++++++++++++++++ 12 files changed, 381 insertions(+), 2 deletions(-) create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/AlbumRepository.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/ArtistRepository.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/MemberRepository.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepository.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListSongRepository.java create mode 100644 YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/SongRepository.java create mode 100644 YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/MemberRepositoryTest.java create mode 100644 YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepositoryTest.java create mode 100644 YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/SongRepositoryTest.java diff --git a/README.md b/README.md index 679b53a..75cd4fe 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,32 @@ + +
+week2.JPA-challenge +
+ + ### Member Test + + ![스크린샷 2024-09-15 175143](https://github.com/user-attachments/assets/1e41e414-0290-46dd-8472-0e03f11fecb5) + +
+ + ### Song Test + + ![스크린샷 2024-09-15 234703](https://github.com/user-attachments/assets/43b5ad0c-9570-4345-91af-d601b4d87fe3) + +
+ + ### PlayList Test + + ![스크린샷 2024-09-15 230558](https://github.com/user-attachments/assets/a78a5978-84f3-446c-8c04-3bb14f13fd73) + +
+ +### PlayListSong Test + +![스크린샷 2024-09-15 230546](https://github.com/user-attachments/assets/470d4e9d-5b70-4f6f-9379-c5fffb8e3196) + +
+
diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java index f54971a..a937e09 100644 --- a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/domain/PlayList.java @@ -21,7 +21,9 @@ public class PlayList { @JoinColumn(name = "member_id") private Member member; - public void serMember(Member member) { + public void setMember(Member member) { this.member = member; } + + } diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/AlbumRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/AlbumRepository.java new file mode 100644 index 0000000..cd770d9 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/AlbumRepository.java @@ -0,0 +1,18 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Album; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; + +@Repository +public class AlbumRepository { + + @PersistenceContext + EntityManager em; + + public String save(Album album) { + em.persist(album); + return album.getTitle(); + } +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/ArtistRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/ArtistRepository.java new file mode 100644 index 0000000..ce3421b --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/ArtistRepository.java @@ -0,0 +1,18 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Artist; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; + +@Repository +public class ArtistRepository { + + @PersistenceContext + EntityManager em; + + public String save(Artist artist) { + em.persist(artist); + return artist.getName(); + } +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/MemberRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/MemberRepository.java new file mode 100644 index 0000000..7e3ecad --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/MemberRepository.java @@ -0,0 +1,35 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Member; +import jakarta.persistence.EntityManager; +import jakarta.persistence.NoResultException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class MemberRepository { + + @PersistenceContext + EntityManager em; + + public Long save(Member member) { + if(findByEmail(member.getEmail()) != null){ + throw new IllegalArgumentException("email already exists."); + } + em.persist(member); + return member.getId(); + } + + public Member findByEmail(String email) { + try { + return em.createQuery("select m from Member m where m.email = :email", Member.class) + .setParameter("email", email) + .getSingleResult(); + } catch (NoResultException e) { + return null; + } + } + +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepository.java new file mode 100644 index 0000000..1bca638 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepository.java @@ -0,0 +1,35 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.PlayList; +import com.example.YoungJu_Lee_Spring.domain.PlayListSong; +import com.example.YoungJu_Lee_Spring.domain.Song; +import jakarta.persistence.EntityManager; +import jakarta.persistence.NoResultException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; + +@Repository +public class PlayListRepository { + + @PersistenceContext + EntityManager em; + + public String save(PlayList playlist) { + if(findByName(playlist.getName()) != null){ + throw new IllegalArgumentException("PlayList name already exists."); + } + em.persist(playlist); + return playlist.getName(); + } + + public PlayList findByName(String name) { + try { + return em.createQuery("select p from PlayList p where p.name = :name", PlayList.class) + .setParameter("name", name) + .getSingleResult(); + } catch (NoResultException e) { + return null; + } + } +} + diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListSongRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListSongRepository.java new file mode 100644 index 0000000..30de6a3 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/PlayListSongRepository.java @@ -0,0 +1,32 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.PlayList; +import com.example.YoungJu_Lee_Spring.domain.PlayListSong; +import com.example.YoungJu_Lee_Spring.domain.Song; +import jakarta.persistence.EntityManager; +import jakarta.persistence.NoResultException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; +import java.util.List; + +@Repository +public class PlayListSongRepository { + + @PersistenceContext + EntityManager em; + + public void save(PlayList playList, Song song) { + PlayListSong playListSong = PlayListSong.builder() + .playlist(playList) + .song(song) + .build(); + em.persist(playListSong); + } + + + public List getSongsInPlayList(PlayList playList) { + return em.createQuery("select pls from PlayListSong pls where pls.playlist.id = :id", PlayListSong.class) + .setParameter("id", playList.getId()) + .getResultList(); + } +} diff --git a/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/SongRepository.java b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/SongRepository.java new file mode 100644 index 0000000..03053f5 --- /dev/null +++ b/YoungJu-Lee-Spring/src/main/java/com/example/YoungJu_Lee_Spring/repository/SongRepository.java @@ -0,0 +1,36 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Song; +import jakarta.persistence.EntityManager; +import jakarta.persistence.NoResultException; +import jakarta.persistence.PersistenceContext; +import org.springframework.stereotype.Repository; +import java.util.List; + +@Repository +public class SongRepository { + + @PersistenceContext + EntityManager em; + + public String save(Song song) { + em.persist(song); + return song.getTitle(); + } + + public List findByTitle(String title) { + try { + return em.createQuery("select s from Song s where s.title like :title", Song.class) + .setParameter("title", "%" + title + "%") + .getResultList(); + } catch (NoResultException e) { + System.out.println("result not found."); + return null; + } + } + + public Song findById(String id) { + return em.find(Song.class, id); + } + +} diff --git a/YoungJu-Lee-Spring/src/main/resources/application.yml b/YoungJu-Lee-Spring/src/main/resources/application.yml index 5123cce..47692eb 100644 --- a/YoungJu-Lee-Spring/src/main/resources/application.yml +++ b/YoungJu-Lee-Spring/src/main/resources/application.yml @@ -10,7 +10,7 @@ spring: path: /h2-console # H2 콘솔의 접근 경로 jpa: hibernate: - ddl-auto: create # Hibernate의 DDL 자동 생성 전략 (update, create, create-drop 등) + ddl-auto: update # Hibernate의 DDL 자동 생성 전략 (update, create, create-drop 등) properties: hibernate: format_sql: true diff --git a/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/MemberRepositoryTest.java b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/MemberRepositoryTest.java new file mode 100644 index 0000000..56218b6 --- /dev/null +++ b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/MemberRepositoryTest.java @@ -0,0 +1,46 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Member; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.stereotype.Repository; +import org.springframework.test.annotation.Rollback; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class MemberRepositoryTest { + + @Autowired + MemberRepository memberRepository; + + @Autowired + EntityManager em; + + @Test + @Transactional + @Rollback(false) + public void testMember(){ + // 멤버 생성 + Member member1 = Member.builder() + .name("member1") + .email("ehung1@likelion") + .build(); + + Long savedMember1 = memberRepository.save(member1); + System.out.println(savedMember1); + + Member member2 = Member.builder() + .name("member2") + .email("ehung2@likelion") + .build(); + + Long savedMember2 = memberRepository.save(member2); + System.out.println(savedMember2); + } + +} \ No newline at end of file diff --git a/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepositoryTest.java b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepositoryTest.java new file mode 100644 index 0000000..2a0fd09 --- /dev/null +++ b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/PlayListRepositoryTest.java @@ -0,0 +1,55 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.PlayList; +import com.example.YoungJu_Lee_Spring.domain.PlayListSong; +import com.example.YoungJu_Lee_Spring.domain.Song; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class PlayListRepositoryTest { + + @Autowired + private PlayListRepository playListRepository; + + @Autowired + private SongRepository songRepository; + + @Autowired + private PlayListSongRepository playListSongRepository; + + @Autowired + EntityManager em; + + @Test + @Transactional + @Rollback(false) + public void testPlayList(){ + // 플레이리스트 생성 + PlayList myPlayList = PlayList.builder() + .name("멋사할 때 듣기 좋은 플레이리스트") + .description("과제 1분 컷 가능") + .build(); + + playListRepository.save(myPlayList); + + // 플레이리스트에 노래 추가 + List result = songRepository.findByTitle("사자"); + if(!result.isEmpty()){ + playListSongRepository.save(myPlayList, result.get(0)); + } + + // 플레이리스트에 담긴 노래 출력 + List songs = playListSongRepository.getSongsInPlayList(myPlayList); + for (PlayListSong playListSong : songs){ + System.out.println(playListSong.getSong().getTitle()); + } + } +} \ No newline at end of file diff --git a/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/SongRepositoryTest.java b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/SongRepositoryTest.java new file mode 100644 index 0000000..d8fda38 --- /dev/null +++ b/YoungJu-Lee-Spring/src/test/java/com/example/YoungJu_Lee_Spring/repository/SongRepositoryTest.java @@ -0,0 +1,73 @@ +package com.example.YoungJu_Lee_Spring.repository; + +import com.example.YoungJu_Lee_Spring.domain.Album; +import com.example.YoungJu_Lee_Spring.domain.Artist; +import com.example.YoungJu_Lee_Spring.domain.Song; +import jakarta.persistence.EntityManager; +import jakarta.transaction.Transactional; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; + +import java.util.Scanner; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class SongRepositoryTest { + + @Autowired + private SongRepository songRepository; + + @Autowired + EntityManager em; + + @Test + @Transactional() + @Rollback(false) + public void testSong(){ + // 인스턴스 생성 + Artist artist = Artist.builder() + .name("사자") + .build(); + + Album album = Album.builder() + .title("사자모음집") + .artist(artist) + .build(); + + Song song = Song.builder() + .title("멋쟁이사자") + .artist(artist) + .album(album) + .build(); + + Song song2 = Song.builder() + .title("사자어흥") + .artist(artist) + .album(album) + .build(); + + String savedSong = songRepository.save(song); + String savedSong2 = songRepository.save(song2); + System.out.println(savedSong); + System.out.println(savedSong2); + + // 노래 검색 테스트 + //Scanner input = new Scanner(System.in); + //System.out.println("Enter title of the song: "); + //String title = input.nextLine(); + + // 사용자 입력 관련해서 인텔리제이 오류로 일단 이렇게 처리 + List result = songRepository.findByTitle("사자"); + + System.out.println("results : "); + for (Song s : result) { + System.out.println("Title: " + s.getTitle() + ", Album: " + s.getAlbum().getTitle() + ", Artist: " + s.getArtist().getName()); + } + + } +} \ No newline at end of file