1

I'm trying to get information from the table in database, but I get a bit of information. I have 2 foreign keys(specialty, faculty) in the table and for those keys, I get nothing in my angular app. I tried to use class in angular for those fields, but this doesn't help. I checked my returned list in the spring controller and found all fields are correct. How to get those fields?

My tables in the spring:

Student table

@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;
    }
}

Specialty table

@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;
    }
}

Faculty table

@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 from 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;
}

Code in component:

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 {
  }
}

Service:

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: First code doesnt work, second works but fields specialty, faculty are empty

<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>
Saliery
  • 155
  • 5
  • 16
  • You have @JsonIgnore on those fields in your student table. Wouldn't that stop them from being serialized and therefore not return as you are expecting them to? – canpan14 Mar 11 '20 at 13:50
  • @canpan14 If I remove this, I get stackoverflow error – Saliery Mar 11 '20 at 14:04
  • Makes sense because a Faculty contains a list of students, fun times :D Try this solution out that uses JsonManagedReferences or JsonBackReference https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue/18288939#18288939 – canpan14 Mar 11 '20 at 14:21
  • @canpan14 I tried JsonManagedReferences and JsonBackReference and I didnt get information about those fields. My console log from angular: {{numberzachetka: 100001, fio: "fio", entrydate: "2020-01-01", course: 1, numbergroup: 6101} – Saliery Mar 11 '20 at 14:49
  • There are no faculty, specialty fields – Saliery Mar 11 '20 at 14:55
  • Just to make sure, you took out `@JsonIgnore` on the `@JoinColumn`'s and tried replacing them with `@JsonBackReference` . And then on the Specialty and Faculty table you added `@JsonBackReference` instead of `@JsonIgnore` – canpan14 Mar 11 '20 at 15:12
  • 1
    @canpan14 I solved using @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="number") and @JsonIgnoreProperties("faculty") for Faculty entity and etc – Saliery Mar 11 '20 at 16:13
  • @canpan14 but I have another problem, the first element of specialty is [object Object], what is it???? other elements are normal number – Saliery Mar 11 '20 at 16:15
  • Awesome! Nothing to be worried about. First I would check the network tab in the browser console and make sure it's returned proper data. [object Object] is the default conversion of a javascript object to a string. `stud.specialty` is an object itself, not a plain string, so it can't just display it on screen. You would need to do `stud.specialty.name` or something like that. If you console.log the student you will likely see the entire object structure. – canpan14 Mar 11 '20 at 18:44

0 Answers0