Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » lazy loading problem
lazy loading problem [message #755295] Mon, 07 November 2011 23:39 Go to next message
George Castrey is currently offline George CastreyFriend
Messages: 4
Registered: November 2011
Junior Member
While trying to do some tests on lazy loading, to check if i'm understanding it well, i got totally confused.

Here's the entities i'm using on my test:

@Entity
public class Family {
@Id
@SequenceGenerator(name="Family_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Family_SEQ")
private int id;


@OneToMany(mappedBy="family", fetch=FetchType.LAZY)
private Set<Person> members;

//getters & setters

public String toString(){
String s="";
for(Person p:getMembers()){
s+=p.getFirstName();
}
return s;
}
}


@Entity
public class Person implements Comparable<Person>{
@Id
@SequenceGenerator(name="Person_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Person_SEQ")
private int id;

private String firstName;
private String lastName;

@ManyToOne
private Family family;

//getters &setters
}

this is my main method :

public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = factory.createEntityManager();

Query q = em.createQuery("select f from Family f");
List<Family> families= q.getResultList();

em.clear();
em.close();
factory.close();

for(Family f:families){
System.out.println(f);
}
}

What i understood from lazy loading, is that if an attribute is marked to be fetched lazily, and doesn't get accessed while it's managed, it won't be loaded in memory and any attempt to access it later won't work. Now what confuses me is that the test described above doesn't have any problem when accessing the lazy members attribute through the detached Family list, even after closing the EM and the EMF ! ... Is that normal? Am-i miss-understanding the lazy loading concept?

Note : I'm using a J2SE environment with an embedded DB.

Thanks in Advance
George
Re: lazy loading problem [message #755570 is a reply to message #755295] Tue, 08 November 2011 20:14 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
EclipseLink has a feature that allows for accessing lazy attributes as long as the context is still available - so as long as the entity hasn't been serialized and the factory is still open.

In your case though, the factory should be closed. It could be that logging settings are calling the toString method prior to the em and factory being closed. It is a bad idea to print relationships in toString methods since they can result in cascading and just unexpected database hits.

Turning on EclipseLink logging to Finest will show the lifecycle of the EM and EMF in your test case, and show when the query to bring in the relationship is occuring.

Best Regards,
Chris
Re: lazy loading problem [message #755572 is a reply to message #755295] Tue, 08 November 2011 20:14 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
EclipseLink has a feature that allows for accessing lazy attributes as long as the context is still available - so as long as the entity hasn't been serialized and the factory is still open.

In your case though, the factory should be closed. It could be that logging settings are calling the toString method prior to the em and factory being closed. It is a bad idea to print relationships in toString methods since they can result in cascading and just unexpected database hits.

Turning on EclipseLink logging to Finest will show the lifecycle of the EM and EMF in your test case, and show when the query to bring in the relationship is occuring.

Best Regards,
Chris
Re: lazy loading problem [message #755603 is a reply to message #755572] Tue, 08 November 2011 22:14 Go to previous message
George Castrey is currently offline George CastreyFriend
Messages: 4
Registered: November 2011
Junior Member
I've Turned on EclipseLink logging to Finest, and figured out that after the factory had been closed, eclipselink did reconnect to the database, then querried for each relationship.

Thanks to you, i understood every thing. Thank you very much Smile
Re: lazy loading problem [message #755604 is a reply to message #755572] Tue, 08 November 2011 22:14 Go to previous message
George Castrey is currently offline George CastreyFriend
Messages: 4
Registered: November 2011
Junior Member
I've Turned on EclipseLink logging to Finest, and figured out that after the factory had been closed, eclipselink did reconnect to the database, then querried for each relationship.

Thanks to you, i understood every thing. Thank you very much :)
Previous Topic:Failed to map sql result set when execute stored procedure returning cursor
Next Topic:StackOverFlow
Goto Forum:
  


Current Time: Fri Apr 26 04:56:27 GMT 2024

Powered by FUDForum. Page generated in 0.03550 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top