We are facing problems with how hibernate handles multiple inserts.
First, We have several instances of a class where the primary key is not auto-generated. We save them in a loop then call flush. But hibernate creates separate insert queries as shown in hibernate log (show_sql). Is this normal? Can we make it use a single insert rather than multiple ones?
Second, Look at the following code:
public void saveCourse() {
Course course = new Course();
List<Student> studentList = new ArraryList();
studentList.add(getStudentService().save(new Student(“hasan”,”askari”)));
studentList.add(getStudentService().save(new Student(“ali”,”alavi”)));
studentList.add(getStudentService().save(Student(“reza”,”rezaei”)));
course.setStudents(studentList);
getCourseService().save(course);
}
When the course object is being saved hibernate create separate inserts for table course_student. Why?
Here are our classes:
@Entity
@Table(name = "student ")
public class Student {
private Long id;
private String name;
private String family;
….
}
@Entity
@Table(name = "course ")
public class Course {
private Long id;
private List<Student> students;
@OneToMany
@JoinTable(name = "course_student")
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
Jdbc batch size is made: hibernate.jdbc.batch_size=100 and we flush the session after every 100 saves.