-1
Related to that question I asked earlier about Hibernate
I am building a Restful API, and making a Join with 2 tables. However I would like to bring only some attributes from my Join table.
Today my query results in the following:
[{"id":1,"name":"Java","schedule":"Monday","teacher":{"id":1,"name":"John","phoneNumber":"35483"}}]
But I’d like you to come this way:
[{"id":1,"name":"Java","schedule":"Monday","teacher":{"name":"John"}}]
That is, only the "name" of "Teacher"
Query:
public interface CoursesRepository extends CrudRepository<Course, Integer>{
@Query("select c, t.name as teste from Course c join c.teacher t ")
public List<Course> findAllCoursesAndTeachers();
}
Teacher class
@Entity
public class Teacher {
@Id
@Column(name="teacher_id")
private int id;
@Column(name="teacher_name")
@Access(AccessType.PROPERTY)
private String name;
@Column(name="teacher_phone")
private String phoneNumber;
@OneToMany(mappedBy="teacher")
private List<Course> courses;
public Teacher() {
courses = new ArrayList<>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/*
@OneToMany(mappedBy="teacher")
public List<Course> getCourses() {
return courses;
}
*/
}
Course class
@Entity
public class Course {
@Id
@Column(name="course_id")
private int id;
@Column(name="course_name")
private String name;
@Column(name="course_schedule")
private String schedule;
@ManyToOne
@JoinColumn(name = "teacher_id")
private Teacher teacher;
public Course() {
}
/*
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="teacher_id")
public Teacher getTeacher() {
return teacher;
}
*/
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSchedule() {
return schedule;
}
public void setSchedule(String schedule) {
this.schedule = schedule;
}
}
I tried to use the @Access(AccessType.PROPERTY)
but it had no effect