2

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

George Casttrey
  • 427
  • 1
  • 6
  • 16

1 Answers1

3

Either memory is a concern, or it is not. Are you running out of memory? If you can have all Teachers in memory, then you can probably have all Groups as well.

Otherwise, if memory is a concern, then you may want to rethink you design.

Typically a request or transactional model is used. An EntityManager will only live for the duration of a request or a transaction. At the end of each transaction a new EntityManager is created, or the em is cleared.

Also, instead of holding onto all instances of a class, the instances are normally queried when required, normally allowing the user to filter the result.

If you instead want to continue to use a single long lived EntityManager, you could refresh the Students (set refresh on the all students query) to revert the references to their groups.

James
  • 17,965
  • 11
  • 91
  • 146
  • "then you can probably have all Groups as well." the Teacher Groups example is just a simplified example to illustrate my problem without entering in the details, so ... yes memory is a concern for my project. Now about the single/multiple EMs, i did a fair research and asked about it in some forums (including stackOverFlow [link](http://stackoverflow.com/questions/7491761/jpa-on-a-desktop-swing-application)), and ended up making the single-em choice. – George Casttrey Nov 07 '11 at 16:23
  • The refresh suggestion seems to be a good idea, but there's something that confuses me. After some tests (with my J2SE environment and my embedded DB), i figured out the lazy loading isn't running properly. That is after querying for the teachers list, i detached (By mean of EntityManageImpl.detach()) each teacher without accessing the groups attribute. Now the groups attribute is supposed to be empty, because the teacher has been detached before accessing it... and surprise groups is loaded in memory and i can show all of its elements! Does this have any thing to do with the embedded DB? – George Casttrey Nov 07 '11 at 16:44
  • i've even closed the EM and the EMFactory just after the getResultList() instruction, and the groups still are accessible ! Am-I miss-understanding the lazy loading ? :( – George Casttrey Nov 07 '11 at 21:36
  • concerning the confision about the lazy loading [here](http://stackoverflow.com/questions/8044321/confused-about-lazy-loading/8057259#8057259)'s the answer. "Otherwise, if memory is a concern, then you may want to rethink you design.", thanks for the tip, i did rethink my design and [here](http://stackoverflow.com/questions/7491761/jpa-on-a-desktop-swing-application)'s the new design i'll adopt :) – George Casttrey Nov 09 '11 at 13:48