В настоящее время я пытаюсь создать связь между классом ученика и классом дисциплины.Вот что я делаю:
@ Публичный класс службы StudentRegistrationService реализует ApplicationRunner {
private final StudentRepository repository;
private final DisciplineRepository repository1;
public StudentRegistrationService(StudentRepository repository, DisciplineRepository repository1) {
this.repository = repository;
this.repository1 = repository1;
}
@Override
public void run(ApplicationArguments args) throws Exception {
Scanner scanner = new Scanner(System.in);
while (true) {
String input = scanner.nextLine();
String[] split = input.split(" ");
String command = split[0];
switch (command.toUpperCase()) {
case "END":
System.out.println("bye bye");
return;
case "STUDLIST":
printAllStudents();
break;
case "STUDNEW":
String[] studentDetails = Arrays.copyOfRange(split, 1, split.length);
createNewStudent(studentDetails);
break;
case "DISCLIST":
printAllDisciplines();
break;
case "DISCNEW":
String[] disciplineDetails = Arrays.copyOfRange(split, 1, split.length);
createNewDiscipline(disciplineDetails);
break;
case "STUDDISC":
String[] studentAndDisciplineId = Arrays.copyOfRange(split, 1, split.length);
studendAndDiscipline(studentAndDisciplineId);
break;
default:
System.out.println("UNKNOWN command, try again!");
break;
}
}
}
private void createNewDiscipline(String[] details) {
int hours=Integer.parseInt(details[1]);
repository1.save(new Discipline(details[0],hours));
}
private void printAllDisciplines() {
Iterable<Discipline> allDisciplines = repository1.findAll();
for (Discipline discipline : allDisciplines) {
System.out.println(discipline);
}
}
private void createNewStudent(String[] details) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate localDate = LocalDate.parse(details[1], dtf);
repository.save(new Student(details[0],localDate));
}
private void printAllStudents() {
Iterable<Student> allStudents = repository.findAll();
for (Student student : allStudents) {
System.out.println(student);
}
}
private void studendAndDiscipline(String[] details) {
int studentId=Integer.parseInt(details[1]);
int disciplineId=Integer.parseInt(details[2]);
Student student=repository.findById(studentId);
Discipline discipline=repository1.findById(disciplineId);
student.addDiscipline(discipline);
}
}
В studentAndDiscipline мне нужно создать связь между учеником и учеником.дисциплина как по удостоверению личности, но я делаю это неправильно.Я попытался использовать Optional <>, но он тоже не работает.
Мой класс ученика: @Entity class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private LocalDate enrollmentDate;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
List<Discipline> disciplines;
private Student() {
// Required by Hibernate
}
public Student(String name, LocalDate enrollmentDate) {
this.name = name;
this.enrollmentDate = enrollmentDate;
}
void addDiscipline(Discipline discipline) {
this.disciplines.add(discipline);
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", enrollmentDate=" + enrollmentDate +
", disciplines=" + disciplines +
'}';
}
}
Мой дисциплина:@Entity class Discipline {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
String name;
int credits;
private Discipline() {
// Required by Hibernate
}
public Discipline(String name, int credits) {
this.name = name;
this.credits = credits;
}
@Override
public String toString() {
return "Discipline{" +
"id=" + id +
", name='" + name + '\'' +
", credits=" + credits +
'}';
}
}
Мой репозиторий для учеников:
interface StudentRepository extends CrudRepository<Student, Integer> {
}
Мой репозиторий для дисциплин:
interface DisciplineRepository extends CrudRepository<Discipline, Integer> {
}