From fa34f76ad8ebccb96012b45a4207532846cfa03f Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Tue, 23 Jul 2024 23:19:45 +1000 Subject: Define JPA entities and repositories and H2 DB --- pom.xml | 10 +++ .../com/stileeducation/markr/entity/Student.java | 92 ++++++++++++++++++++++ .../java/com/stileeducation/markr/entity/Test.java | 80 +++++++++++++++++++ .../stileeducation/markr/entity/TestResult.java | 80 +++++++++++++++++++ .../markr/repository/StudentRepository.java | 10 +++ .../markr/repository/TestRepository.java | 10 +++ .../markr/repository/TestResultRepository.java | 19 +++++ src/main/resources/application.properties | 10 +++ 8 files changed, 311 insertions(+) create mode 100644 src/main/java/com/stileeducation/markr/entity/Student.java create mode 100644 src/main/java/com/stileeducation/markr/entity/Test.java create mode 100644 src/main/java/com/stileeducation/markr/entity/TestResult.java create mode 100644 src/main/java/com/stileeducation/markr/repository/StudentRepository.java create mode 100644 src/main/java/com/stileeducation/markr/repository/TestRepository.java create mode 100644 src/main/java/com/stileeducation/markr/repository/TestResultRepository.java diff --git a/pom.xml b/pom.xml index 491fe0d..0281f72 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,16 @@ org.assertj assertj-core + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + diff --git a/src/main/java/com/stileeducation/markr/entity/Student.java b/src/main/java/com/stileeducation/markr/entity/Student.java new file mode 100644 index 0000000..9ef2091 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/entity/Student.java @@ -0,0 +1,92 @@ +package com.stileeducation.markr.entity; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +@Table(name = "students") +public class Student { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "first_name", nullable = false) + private String firstName; + + @Column(name = "last_name", nullable = false) + private String lastName; + + @Column(name = "student_number", nullable = false, unique = true) + private String studentNumber; + + @OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval = true) + private Set testResults = new HashSet<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getStudentNumber() { + return studentNumber; + } + + public void setStudentNumber(String studentNumber) { + this.studentNumber = studentNumber; + } + + public Set getTestResults() { + return testResults; + } + + public void setTestResults(Set testResults) { + this.testResults = testResults; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Student student = (Student) o; + return Objects.equals(id, student.id) && Objects.equals(firstName, student.firstName) && Objects.equals(lastName, student.lastName) && Objects.equals(studentNumber, student.studentNumber) && Objects.equals(testResults, student.testResults); + } + + @Override + public int hashCode() { + return Objects.hash(id, firstName, lastName, studentNumber, testResults); + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", studentNumber='" + studentNumber + '\'' + + ", testResults=" + testResults + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/stileeducation/markr/entity/Test.java b/src/main/java/com/stileeducation/markr/entity/Test.java new file mode 100644 index 0000000..3729e1b --- /dev/null +++ b/src/main/java/com/stileeducation/markr/entity/Test.java @@ -0,0 +1,80 @@ +package com.stileeducation.markr.entity; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +@Table(name = "tests") +public class Test { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "test_id", nullable = false, unique = true) + private String testId; + + @Column(name = "marks_available", nullable = false) + private Integer marksAvailable; + + @OneToMany(mappedBy = "test", cascade = CascadeType.ALL, orphanRemoval = true) + private Set testResults = new HashSet<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTestId() { + return testId; + } + + public void setTestId(String testId) { + this.testId = testId; + } + + public Integer getMarksAvailable() { + return marksAvailable; + } + + public void setMarksAvailable(Integer marksAvailable) { + this.marksAvailable = marksAvailable; + } + + public Set getTestResults() { + return testResults; + } + + public void setTestResults(Set testResults) { + this.testResults = testResults; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Test test = (Test) o; + return Objects.equals(id, test.id) && Objects.equals(testId, test.testId) && Objects.equals(marksAvailable, test.marksAvailable) && Objects.equals(testResults, test.testResults); + } + + @Override + public int hashCode() { + return Objects.hash(id, testId, marksAvailable, testResults); + } + + @Override + public String toString() { + return "Test{" + + "id=" + id + + ", testId='" + testId + '\'' + + ", marksAvailable=" + marksAvailable + + ", testResults=" + testResults + + '}'; + } +} diff --git a/src/main/java/com/stileeducation/markr/entity/TestResult.java b/src/main/java/com/stileeducation/markr/entity/TestResult.java new file mode 100644 index 0000000..cb7cea5 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/entity/TestResult.java @@ -0,0 +1,80 @@ +package com.stileeducation.markr.entity; + +import jakarta.persistence.*; + +import java.util.Objects; + +@Entity +@Table(name = "test_results") +public class TestResult { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "student_id", nullable = false) + private Student student; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "test_id", nullable = false) + private Test test; + + @Column(name = "marks_awarded", nullable = false) + private Integer marksAwarded; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Test getTest() { + return test; + } + + public void setTest(Test test) { + this.test = test; + } + + public Integer getMarksAwarded() { + return marksAwarded; + } + + public void setMarksAwarded(Integer marksAwarded) { + this.marksAwarded = marksAwarded; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestResult that = (TestResult) o; + return Objects.equals(id, that.id) && Objects.equals(student, that.student) && Objects.equals(test, that.test) && Objects.equals(marksAwarded, that.marksAwarded); + } + + @Override + public int hashCode() { + return Objects.hash(id, student, test, marksAwarded); + } + + @Override + public String toString() { + return "TestResult{" + + "id=" + id + + ", student=" + student + + ", test=" + test + + ", marksAwarded=" + marksAwarded + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/stileeducation/markr/repository/StudentRepository.java b/src/main/java/com/stileeducation/markr/repository/StudentRepository.java new file mode 100644 index 0000000..2d27644 --- /dev/null +++ b/src/main/java/com/stileeducation/markr/repository/StudentRepository.java @@ -0,0 +1,10 @@ +package com.stileeducation.markr.repository; + +import com.stileeducation.markr.entity.Student; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface StudentRepository extends JpaRepository { + Optional findByStudentNumber(String studentNumber); +} diff --git a/src/main/java/com/stileeducation/markr/repository/TestRepository.java b/src/main/java/com/stileeducation/markr/repository/TestRepository.java new file mode 100644 index 0000000..f30ae0b --- /dev/null +++ b/src/main/java/com/stileeducation/markr/repository/TestRepository.java @@ -0,0 +1,10 @@ +package com.stileeducation.markr.repository; + +import com.stileeducation.markr.entity.Test; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface TestRepository extends JpaRepository { + Optional findByTestId(String testId); +} diff --git a/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java b/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java new file mode 100644 index 0000000..5810f8e --- /dev/null +++ b/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java @@ -0,0 +1,19 @@ +package com.stileeducation.markr.repository; + +import com.stileeducation.markr.entity.Student; +import com.stileeducation.markr.entity.Test; +import com.stileeducation.markr.entity.TestResult; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; +import java.util.Optional; + +public interface TestResultRepository extends JpaRepository { + + Optional findByStudentAndTest(Student student, Test test); + + @Query("SELECT tr FROM TestResult tr WHERE tr.test.testId = :testId") + List findAllByTestId(@Param("testId") String testId); +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7e171c2..788b1a5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,11 @@ spring.application.name=markr + +# H2 Database configuration +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console +spring.jpa.hibernate.ddl-auto=update -- cgit v1.2.3