Осталось добавить взаимодействие между сущностями
This commit is contained in:
parent
8595331a1d
commit
97d5ae0cf4
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -2,6 +2,7 @@ package ru.IP_LabWorks.IP.University.Model;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -12,12 +13,19 @@ public class Group {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
@OneToMany
|
@OneToMany(fetch = FetchType.EAGER)
|
||||||
@JoinTable(name = "students_in_groups",
|
|
||||||
joinColumns = @JoinColumn(name = "group_id"),
|
|
||||||
inverseJoinColumns = @JoinColumn(name = "student_id"))
|
|
||||||
private List<Student> students;
|
private List<Student> students;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
private List<Subject> subjects;
|
||||||
|
|
||||||
|
public Group() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Group(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() { return id; }
|
public Long getId() { return id; }
|
||||||
public String getName() { return name; }
|
public String getName() { return name; }
|
||||||
public void setName(String name) { this.name = name; }
|
public void setName(String name) { this.name = name; }
|
||||||
@ -42,4 +50,22 @@ public class Group {
|
|||||||
", name='" + name + '\'' +
|
", name='" + name + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Student> getStudents(){
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addStudent(Student student){
|
||||||
|
if (students == null)
|
||||||
|
students = new ArrayList<>();
|
||||||
|
students.add(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Subject> getSubjects() { return subjects; }
|
||||||
|
|
||||||
|
public void addSubject(Subject subject){
|
||||||
|
if (subjects == null)
|
||||||
|
subjects = new ArrayList<>();
|
||||||
|
subjects.add(subject);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ru.IP_LabWorks.IP.University.Model;
|
package ru.IP_LabWorks.IP.University.Model;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -12,11 +13,6 @@ public class Student {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private Integer age;
|
private Integer age;
|
||||||
@ManyToMany(cascade = CascadeType.ALL)
|
|
||||||
@JoinTable(name = "student_subject",
|
|
||||||
joinColumns = @JoinColumn(name = "student_id"),
|
|
||||||
inverseJoinColumns = @JoinColumn(name = "subject_id"))
|
|
||||||
private List<Subject> subjects;
|
|
||||||
|
|
||||||
public Student() {
|
public Student() {
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ public class Subject {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
@ManyToMany(mappedBy = "subjects")
|
@ManyToMany
|
||||||
private List<Student> students;
|
private List<Group> groups;
|
||||||
|
|
||||||
public Subject() {
|
public Subject() {
|
||||||
}
|
}
|
||||||
@ -46,4 +46,9 @@ public class Subject {
|
|||||||
", name='" + name + '\'' +
|
", name='" + name + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Group> getGroups() {
|
||||||
|
return groups;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,84 @@
|
|||||||
package ru.IP_LabWorks.IP.University.Service;
|
package ru.IP_LabWorks.IP.University.Service;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Group;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Student;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Subject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
public class GroupService {
|
public class GroupService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Group addGroup(String name){
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Group name is null or empty");
|
||||||
|
}
|
||||||
|
final Group group = new Group(name);
|
||||||
|
em.persist(group);
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Group findGroup(Long id){
|
||||||
|
final Group group = em.find(Group.class, id);
|
||||||
|
if (group == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Group with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Group> findAllGroups() {
|
||||||
|
return em.createQuery("select s from Group s", Group.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Group updateGroup(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Group name is null or empty");
|
||||||
|
}
|
||||||
|
final Group currentGroup = findGroup(id);
|
||||||
|
currentGroup.setName(name);
|
||||||
|
return em.merge(currentGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Group deleteGroup(Long id) {
|
||||||
|
final Group currentGroup = findGroup(id);
|
||||||
|
em.remove(currentGroup);
|
||||||
|
return currentGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllGroups() {
|
||||||
|
em.createQuery("delete from Group").executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Group deleteStudentInGroup(Long id, Student student) {
|
||||||
|
final Group currentGroup = findGroup(id);
|
||||||
|
final Student currentStudent = em.find(Student.class, student.getId());
|
||||||
|
currentGroup.getStudents().remove(currentStudent);
|
||||||
|
em.merge(currentStudent);
|
||||||
|
return em.merge(currentGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Student> findStudentsInGroup(Long id){
|
||||||
|
final Group findGroup = findGroup(id);
|
||||||
|
return findGroup.getStudents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Subject> findSubjectsInGroup(Long id){
|
||||||
|
final Group findGroup = findGroup(id);
|
||||||
|
return findGroup.getSubjects();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package ru.IP_LabWorks.IP.University.Service;
|
package ru.IP_LabWorks.IP.University.Service;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import jakarta.persistence.PersistenceContext;
|
import jakarta.persistence.PersistenceContext;
|
||||||
@ -8,6 +7,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import ru.IP_LabWorks.IP.University.Model.Student;
|
import ru.IP_LabWorks.IP.University.Model.Student;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Subject;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
package ru.IP_LabWorks.IP.University.Service;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Group;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Subject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SubjectService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Subject addSubject(String name){
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Subject name is null or empty");
|
||||||
|
}
|
||||||
|
final Subject subject = new Subject(name);
|
||||||
|
em.persist(subject);
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Subject findSubject(Long id){
|
||||||
|
final Subject subject = em.find(Subject.class, id);
|
||||||
|
if (subject == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Subject with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Subject> findAllSubjects() {
|
||||||
|
return em.createQuery("select s from Subject s", Subject.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Subject updateSubject(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Subject name is null or empty");
|
||||||
|
}
|
||||||
|
final Subject currentSubject = findSubject(id);
|
||||||
|
currentSubject.setName(name);
|
||||||
|
return em.merge(currentSubject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Subject deleteSubject(Long id) {
|
||||||
|
final Subject currentSubject = findSubject(id);
|
||||||
|
em.remove(currentSubject);
|
||||||
|
return currentSubject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllSubjects() {
|
||||||
|
em.createQuery("delete from Subject").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
80
src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java
Normal file
80
src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package ru.IP_LabWorks.IP;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Group;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Student;
|
||||||
|
import ru.IP_LabWorks.IP.University.Service.GroupService;
|
||||||
|
import ru.IP_LabWorks.IP.University.Service.StudentService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class GroupServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private GroupService groupService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
final Group group = groupService.addGroup("ПИбд-21");
|
||||||
|
Assertions.assertNotNull(group.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
final Group group = groupService.addGroup("ПИбд-21");
|
||||||
|
|
||||||
|
final Group findGroup = groupService.findGroup(group.getId());
|
||||||
|
Assertions.assertEquals(group, findGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
final Group group1 = groupService.addGroup("ПИбд-21");
|
||||||
|
final Group group2 = groupService.addGroup("ПИбд-22");
|
||||||
|
|
||||||
|
final List<Group> groups = groupService.findAllGroups();
|
||||||
|
Assertions.assertEquals(groups.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestUpdateGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
Group group = groupService.addGroup("ПИбд-21");
|
||||||
|
|
||||||
|
group = groupService.updateGroup(group.getId(), "ПИбд-22");
|
||||||
|
Assertions.assertEquals(group.getName(), "ПИбд-22");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
Group group = groupService.addGroup("ПИбд-21");
|
||||||
|
groupService.deleteGroup(group.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> {
|
||||||
|
groupService.findGroup(group.getId());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindStudentsInGroup(){
|
||||||
|
groupService.deleteAllGroups();
|
||||||
|
Student student1 = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19);
|
||||||
|
Group group = groupService.addGroup("ПИбд-21");
|
||||||
|
group.addStudent(student1);
|
||||||
|
group.addStudent(student2);
|
||||||
|
final List<Student> students = groupService.findStudentsInGroup(group.getId());
|
||||||
|
System.out.println("DFGSDFGSDFGSDFG" + students.size());
|
||||||
|
Assertions.assertEquals(students.size(), 2);
|
||||||
|
}
|
||||||
|
}
|
65
src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java
Normal file
65
src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package ru.IP_LabWorks.IP;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Student;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Subject;
|
||||||
|
import ru.IP_LabWorks.IP.University.Service.StudentService;
|
||||||
|
import ru.IP_LabWorks.IP.University.Service.SubjectService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class StudentServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddStudent(){
|
||||||
|
studentService.deleteAllStudent();
|
||||||
|
final Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
Assertions.assertNotNull(student.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindStudent(){
|
||||||
|
studentService.deleteAllStudent();
|
||||||
|
final Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
|
||||||
|
final Student findStudent = studentService.findStudent(student.getId());
|
||||||
|
Assertions.assertEquals(student, findStudent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllStudent(){
|
||||||
|
studentService.deleteAllStudent();
|
||||||
|
final Student student1 = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
|
||||||
|
final Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19);
|
||||||
|
|
||||||
|
final List<Student> students = studentService.findAllStudents();
|
||||||
|
Assertions.assertEquals(students.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestUpdateStudent(){
|
||||||
|
studentService.deleteAllStudent();
|
||||||
|
Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
|
||||||
|
student = studentService.updateStudent(student.getId(), "Емельянов Артём Сергеевич", 19);
|
||||||
|
Assertions.assertEquals(student.getName(), "Емельянов Артём Сергеевич");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteStudent(){
|
||||||
|
studentService.deleteAllStudent();
|
||||||
|
Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||||
|
studentService.deleteStudent(student.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> {
|
||||||
|
studentService.findStudent(student.getId());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
62
src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java
Normal file
62
src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package ru.IP_LabWorks.IP;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import ru.IP_LabWorks.IP.University.Model.Subject;
|
||||||
|
import ru.IP_LabWorks.IP.University.Service.SubjectService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class SubjectServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private SubjectService subjectService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestAddSubject(){
|
||||||
|
subjectService.deleteAllSubjects();
|
||||||
|
final Subject subject = subjectService.addSubject("Философия");
|
||||||
|
Assertions.assertNotNull(subject.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindSubject(){
|
||||||
|
subjectService.deleteAllSubjects();
|
||||||
|
final Subject subject = subjectService.addSubject("Философия");
|
||||||
|
|
||||||
|
final Subject findSubject = subjectService.findSubject(subject.getId());
|
||||||
|
Assertions.assertEquals(subject, findSubject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestFindAllSubject(){
|
||||||
|
subjectService.deleteAllSubjects();
|
||||||
|
final Subject subject1 = subjectService.addSubject("Философия");
|
||||||
|
final Subject subject2 = subjectService.addSubject("Математика");
|
||||||
|
|
||||||
|
final List<Subject> subjects = subjectService.findAllSubjects();
|
||||||
|
Assertions.assertEquals(subjects.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestUpdateSubject(){
|
||||||
|
subjectService.deleteAllSubjects();
|
||||||
|
Subject subject = subjectService.addSubject("Философия");
|
||||||
|
|
||||||
|
subject = subjectService.updateSubject(subject.getId(), "Математика");
|
||||||
|
Assertions.assertEquals(subject.getName(), "Математика");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteSubject(){
|
||||||
|
subjectService.deleteAllSubjects();
|
||||||
|
Subject subject = subjectService.addSubject("Математика");
|
||||||
|
subjectService.deleteSubject(subject.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> {
|
||||||
|
subjectService.findSubject(subject.getId());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user