简体   繁体   中英

Try to fetch custom object data from database using spring data jpa from multiple table

I want to get data from multiple table.

public class Student{
    private int id;
    private String name;
    private List<Course> course;
}

public class Course{
    private int id;
    private String name;
    private int studentId;
}

I want to fetch data from student and course table using spring data jpa and map to student object. How can I do that in efficient way?

@Data
@NoArgsConstructor
public class Student{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String name;
    @OneToMany(mappedBy="studentId",cascade=CascadeType.ALL,fetch=FetchType.Eager) 
    private Set<Course> course;
}
   
  1. You May Use Set Instead of List.

  2. Always Use Mapped By in OneToMany Side, If you use it manyToOne side it will create an extra table.

  3. You can use Fetch Type eager or lazy. By default, it is lazy with You have to use @transactional of Lazy.

 @Data @NoArgsConstructor public class Course{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; @ManyToOne @JoinColumn(name="studentId") private int studentId; }

Hope this Answer Solve your query Happy Coding..

Note that the starting point might be wrong. I assume that a student can choose multiple courses and a course can be chosen by multiple students. So it is actually a @ManyToMany relationship but not @ManyToOne or @OneToMany .

You will definitely need a joint table to map their primary keys from two tables into the joint table.

@Entity
@Data
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @EqualsAndHashCode.Exclude
    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinTable(
            name = "courses",
            joinColumns = @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "course_id"))
    private Set<Course> courses;
}
@Entity
@Data
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @JsonIgnore
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToMany(mappedBy = "courses",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private Set<Student> students;
}

Note all the modifications I have here.

  1. For the data persisted into database, Long is a better choice than int . Similarly, eg, use Boolean instead of boolean .
  2. Think the Student as the side managing the many-to-many relationship, and Course as the target side. On the target side, use @JsonIgnore and @ToString.Exclude annotations to avoid an infinite recursion, StackOverflow or OOM.
    @JsonIgnore
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToMany(mappedBy = "courses",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
  1. Use Set instead of List if a student is not supposed to select the exact same course. It ensures that one can still select 2017 fall Maths and 2018 fall Maths , while one cannot select 2017 fall Maths twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM