I'm developing my first EclipseLink J2SE project, and wondering if there's an efficient way to take benefit of the lazy loading even after accessing an object.
Here's an example to illustrate the problem. suppose we have these 2 entities:
@Entity
public class Group {
long id;
@ManyToOne
Teacher teacher;
//getters & setters
}
@Entity
public class Teacher {
long id;
@OneToMany(mappedBy="teacher", fetch=FetchType.LAZY)
List<Group> groups;
//getters & setters
}
To load the list of teachers (which will be shown on a JTable component), i use a static method from a classe called MyPersistenceManager:
public static List<Teacher> loadTeachers(){
Query q=getEM().createQuery("select t from teacher t");
return (List<Teacher>)q.getResultList();
}
and then the returned list will be referenced as an attribute of the TableModel.
Now suppose we want to load the list of groups correponding to the selected teacher each time the selection changes (there's no problem here invoking the getter will do the job), and (here is my problem) in order to preserve memory, clean the memory allocated to these groups when the corresponding teacher gets deselected. Is there any way to do that?
Note : my project as mentioned above is a J2SE project, and is meant to be used locally with an embedded data-base (no network of many clients). I'm using a long-lived persistence-context (EM), with the "eclipselink.persistence-context.reference-mode" property set to "WEAK", and the "shared-cache-mode" set to NONE to disactivate 2nd cache Level (useless in my case). One more thing, you may say that i don't have to worry about memory issues with the WEAK mode, but the truth is that the WEAK mode doesn't touch the referenced entities, and in my case the teachers list is referenced so each groups attribute that gets accessed will stay loaded in memory until the application gets closed ...
Thanks in Advance
George