Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:39:32+00:00 2026-05-26T20:39:32+00:00

While trying to do some tests on lazy loading, to check if i’m understanding

  • 0

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
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
private int id;

private String firstName;
private String lastName;

@ManyToOne
private Family family;

//getters &setters
}

here’s 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. My provider is EclipseLink

Thanks in Advance
George

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:39:32+00:00Added an answer on May 26, 2026 at 8:39 pm

    Check that your toString method is not triggered before the factory is closed, such as if the entity is being logged. I would not recommend triggering relationship in a toString method as this is error prone and can be triggered unexpectedly. Turning on EclipseLink logging will help show you where it gets accessed in the factory’s lifecycle, assuming it is not part of the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got an error today while trying to do some formatting to existing code.
I keep getting this error while trying to modify some tables. Here's my code:
I am trying to run some unit tests using NUnit while I have the
I am having some problems while trying to create integration tests with Selenium and
While writing some test cases, and some of the tests check for the result
I am working on a little mini compiler while trying to learn some MIPS
While trying to compile my project, that uses some third party headers, with mingw
I am getting some problems while trying to use this regex: (public +function|private +function|function)
I have been trying to find some resource on this topic for a while
I'm trying to load page and then remove some elements in it. While doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.