How to popular only a part of the object with a query in Hibernate

Asked

Viewed 31 times

-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

1 answer

0

You can specify the class that will be populated by the repository via projection.

public interface CoursesRepository extends CrudRepository{

@Query("select  new pacote.completo.CustomCourseDTO(c.id, c.name, c.schedule, t.name) from Course c join c.teacher t ")
public List findAllCoursesAndTeachers();

}
public CustomCourseDTO() {

     private int id;
     private String name;
     private String schedule;
     private TeacherDTO teacher;

    public CustomCourseDTO(int id, String name, String schedule, String teacherName) {

            this.id = id;
            this.name = name;
            this.schedule = schedule;
            this.teacher = new Teacher();
            this.teacher.setName(teacherName);
    }
}
public TeacherDTO() {

        private String name;

        public TeacherDTO(String name) {
                this.name = name;
        }
}

Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections.dtos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.