Skip to content

김현민 1주차 과제 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Grades.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package doit.jpastudy2.repository;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

import doit.jpastudy2.repository.Student;
import java.util.List;

@Entity
@NoArgsConstructor
@Getter
public class Grades {
Comment on lines +12 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자동으로 PK 값 생성하도록 설정해주신 것 좋습니다!

@Id
@GeneratedValue
private int gradeId;

private String subject;

@OneToMany(mappedBy="studentId")
private List<Student> students;

private String grade;
}
8 changes: 8 additions & 0 deletions src/main/java/doit/jpastudy2/repository/GradesRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface GradesRepository extends JpaRepository {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface GradesRepository extends JpaRepository {
public interface GradesRepository extends JpaRepository<Grades, Long> {

JpaRepository를 사용하실 떄는 제네릭 부분에 <Entity 클래스 명, PK의 타입> 형태로 작성해주셔야합니다!


Grades findAllBySubject(String subject);
}
25 changes: 25 additions & 0 deletions src/main/java/doit/jpastudy2/repository/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package doit.jpastudy2.repository;


import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Date;

@Entity
@NoArgsConstructor
@Getter
public class Student {
@Id
private int studentId;
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

학생 학번이라 의도적으로 @GeneratedValue를 사용하지 않고, 직접 PK를 작성하도록 구현한 디테일 너무 좋습니다!

private String studentName;
private Date DateOfBirth;

public Student(int studentId, String studentName, Date dateOfBirth) {
this.studentId = studentId;
this.studentName = studentName;
this.DateOfBirth = dateOfBirth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package doit.jpastudy2.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import doit.jpastudy2.repository.Student;

public interface StudentRepository extends JpaRepository<Student, Long> {

Student findByStudentId(String studentId);
}