Получение информации из таблицы в базе данных angular2 spring - PullRequest
1 голос
/ 11 марта 2020

Я пытаюсь получить информацию из таблицы в базе данных, но я получаю немного информации. У меня есть 2 внешних ключа (специальность, факультет) в таблице, и для этих ключей я ничего не получаю в своем приложении angular. Я пытался использовать класс в angular для этих полей, но это не помогает. Я проверил свой возвращенный список в контроллере пружины и обнаружил, что все поля верны. Как получить эти поля?

Мои таблицы весной:

Ученический стол

@Entity
@Table(name="student")
public class Student  {

    @Id
    @Column(name="numberzachetka", nullable = false)
    private long numberzachetka;

    @Column(name="fiostudent", nullable = false, length = 100)
    private String fio;

    @Temporal(TemporalType.DATE)
    @Column(name = "entrydate", nullable = false)
    private Date entrydate;

    @Column(name="course", nullable = false)
    private int course;

    @Column(name="numbergroup", nullable = false)
    private int numbergroup;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "specialtykey", nullable = false)
    @JsonIgnore
    private Specialty specialty;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "facultynumber", nullable = false)
    @JsonIgnore
    private Faculty faculty;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
    @JsonIgnore
    private Set<Performance> performances;

    public Student(){}

    public Student(long numberzachetka, String fio, Date entrydate, int course, int numbergroup, Specialty specialty, Faculty faculty) {
        this.numberzachetka = numberzachetka;
        this.fio = fio;
        this.entrydate = entrydate;
        this.course = course;
        this.numbergroup = numbergroup;
        this.specialty = specialty;
        this.faculty = faculty;
    }

    public Student(long numberzachetka, String fio, Date entrydate, int course, int numbergroup, Specialty specialty, Faculty faculty, Set<Performance> performances) {
        this.numberzachetka = numberzachetka;
        this.fio = fio;
        this.entrydate = entrydate;
        this.course = course;
        this.numbergroup = numbergroup;
        this.specialty = specialty;
        this.faculty = faculty;
        this.performances = performances;
    }

    public long getNumberzachetka() {
        return numberzachetka;
    }

    public void setNumberzachetka(long numberzachetka) {
        this.numberzachetka = numberzachetka;
    }

    public String getFio() {
        return fio;
    }

    public void setFio(String fio) {
        this.fio = fio;
    }

    public Date getEntrydate() {
        return entrydate;
    }

    public void setEntrydate(Date entrydate) {
        this.entrydate = entrydate;
    }

    public int getCourse() {
        return course;
    }

    public void setCourse(int course) {
        this.course = course;
    }

    public int getNumbergroup() {
        return numbergroup;
    }

    public void setNumbergroup(int numbergroup) {
        this.numbergroup = numbergroup;
    }

    public Specialty getSpecialty() {
        return specialty;
    }

    public void setSpecialty(Specialty specialty) {
        this.specialty = specialty;
    }

    public Faculty getFaculty() {
        return faculty;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }

    public Set<Performance> getPerformances() {
        return performances;
    }

    public void setPerformances(Set<Performance> performances) {
        this.performances = performances;
    }
}

Специальность стол

@Entity
@Table(name="specialty")
public class Specialty {

    @Id
    @Column(name="specialtykey",nullable = false)
    private long key;

    @Column(name="specialtyname",nullable = false, length = 100)
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "specialty")
    @JsonIgnore
    private Set<Student> students;

    public Specialty(){}

    public Specialty(long key, String name) {
        this.key = key;
        this.name = name;
    }

    public Specialty(long key, String name, Set<Student> students) {
        this.key = key;
        this.name = name;
        this.students = students;
    }

    public long getKey() {
        return key;
    }

    public void setKey(long key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }
}

Стол преподавателей

@Entity
@Table(name = "faculty")
public class Faculty {

    @Id
    @Column(name="facultynumber",nullable = false)
    private long number;

    @Column(name="facultyname",nullable = false, length = 50)
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "faculty")
    @JsonIgnore
    private Set<Student> students;

    public Faculty(){}

    public Faculty(long number, String name) {
        this.number = number;
        this.name = name;
    }

    public Faculty(long number, String name, Set<Student> students) {
        this.number = number;
        this.name = name;
        this.students = students;
    }

    public long getNumber() {
        return number;
    }

    public void setNumber(long number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }
}

StudentController:

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("")
public class StudentController {

    @Autowired
    private StudentRepo studentRepo;

    @GetMapping("")
    private List<Student> getAll(){
        List<Student> list = studentRepo.findAll();
        for(Student el : list){
            System.out.println(el.getSpecialty().getKey()+" "+el.getFaculty().getNumber());
        }
        return list;
    }
}

Student.ts от angular

import { DatePipe } from '@angular/common';

export class Student{
  numberzachetka: number;
  fio: string;
  entrydate: Date;
  course: number;
  numbergroup: number;
  specialty: Specialty;
  faculty: Faculty;
}

export class Faculty{
  number: number;
  name: string;
}

export class Specialty{
  key: number;
  name: string;
}

Код в компоненте:

import { Component, OnInit } from '@angular/core';
import {Student} from './student';
import { StudentConnector  } from "./studentconnector.service";

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css'],
    providers: [ StudentConnector ]
})

export class MainComponent implements OnInit {

  private students: Student[]= [];

  page = 1;
  pageSize = 4;

  public getStudents(): Student[] {
    return this.students;
  }

  public getSize(){
    return this.students.length;
  }

  constructor(private studentconnector: StudentConnector) {
      this.studentconnector.getStudents().subscribe(data => this.students=data);

   }

  ngOnInit(): void {
  }
}

Сервис:

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import { map } from 'rxjs/operators';
import {Student} from './student';

@Injectable({
  providedIn: 'root',
})

@Injectable()
export class StudentConnector{

    constructor(private http: HttpClient){ }

    public getStudents() : Observable<Student[]> {
        return  this.http.get<Student[]>('http://localhost:8090');
      }
}

Html: первый код не работает, второй работает, но поля специальности, факультеты пусты

<td>{{stud.fio}}</td>
                <td>{{stud.entrydate}}</td>
                <td>{{stud.course}}</td>
                <td>{{stud.numbergroup}}</td>
                <td>{{stud.specialty.key}}</td>
                <td>{{stud.faculty.number}}</td>

<td>{{stud.fio}}</td>
                <td>{{stud.entrydate}}</td>
                <td>{{stud.course}}</td>
                <td>{{stud.numbergroup}}</td>
                <td>{{stud.specialty}}</td>
                <td>{{stud.faculty}}</td>
...