I have a table "class" which is linked to tables "student" and "teachers". A "class" is linked to multiple students and teachers via foriegn key relationship.
When I use hibernate associations and fetch large number of entities(tried for 5000) i am seeing that it is taking
~4 times more memory than if i just use foreign key place holders. Is there something wrong in hibernate association? Any way to improve this?
This is how the schema is:
Code:
class(id,className)
student(id,studentName,class_id)
teacher(id,teacherName,class_id)
class_id is foreign key..
Case #1 - Hibernate Associations1)in Class Entity , mapped students and teachers as :
Code:
@Entity
@Table(name="class")
public class Class {
private Integer id;
private String className;
private Set<Student> students = new HashSet<Student>();
private Set<Teacher> teachers = new HashSet<Teacher>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "classRef")
@Cascade({ CascadeType.ALL })
@Fetch(FetchMode.SELECT)
@BatchSize(size=500)
public Set<Student> getStudents() {
return students;
}
2)in students and teachers , mapped class as:
Code:
@Entity
@Table(name="student")
public class Student {
private Integer id;
private String studentName;
private Class classRef;
@ManyToOne
@JoinColumn(name = "class_id")
public Class getClassRef() {
return classRef;
}
Query used :
Code:
sessionFactory.openSession().createQuery("from Class where id<5000");
This however was taking a Huge amount of memory.
Case #2- Remove associations and fetch seperately1)No Mapping in class entity
Code:
@Entity
@Table(name="class")
public class Class {
private Integer id;
private String className;
2)Only a placeholder for Foreign key in student, teachers
Code:
@Entity
@Table(name="student")
public class Student {
private Integer id;
private String studentName;
private Integer class_id;
Queries used :
Code:
sessionFactory.openSession().createQuery("from Class where id<5000");
sessionFactory.openSession().createQuery("from Student where class_id = :classId");
sessionFactory.openSession().createQuery("from Teacher where class_id = :classId");
Note - Shown only imp. part of the code. I am measuring memory usage of the fetched entities via JAMM library.