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 6003901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:11:13+00:00 2026-05-23T01:11:13+00:00

In my Web Application I load many Entities, to display them in a table.

  • 0

In my Web Application I load many Entities, to display them in a table. I can click on each tablerow to get the ‘detailed information’ regarding the specific entity.

To me it was obvious to not load the ‘detailed information’ for the tableview, but only when someone wants to see it (clicks a row).

Just setting (fetch = FetchType.LAZY) for these fields didn’t work because the entities get detached after fetching and in my WebApp there will be nulls.

Okay, so the next thing I did was to prevent the detaching by putting my fetch-operations into a StatefulSessionBean with an extended PersistenceContext.

@PersistenceContext(unitName="unitname", type=PersistenceContextType.EXTENDED)
private EntityManager em;

This works but produces strange sideeffects too (most notably ConcurrentAccessExceptions on some page reloads, which I could fix by setting some openjpa-property)
Servlets need their own fetching-ejbs since they don’t team up with SFSBs. Atm most things seem to work okay but I’m expecting the s**t to hit the fan soon.

My question is if I’m on the wrong track. This all seems a little awkward to me. Using a stateful bean when there is no real conversation and a user can leave at any time without triggering some @Remove-Method. Having to close resources on Timeout when the user is long gone, resulting in many open-unused SFSBs.

LazyLoading in general is a quite simple thing but in the Java EE Environment I don’t get how to do it. What is the best practice?

Thank you.

UPDATE

this is how I manually fetch the fields now

@SuppressWarnings("unchecked")
public <T extends BasicEntity> T loadLazyField(T entity, String field) throws NoSuchFieldException {
    if (!typeHasField(entity.getClass(), field)) {
        throw new NoSuchFieldException(entity.getClass().getSimpleName() + " has no field called " + field);
    }

    String queryString = String.format("SELECT x FROM %s x WHERE x = :entity LEFT JOIN FETCH x.%s", entity.getClass()
            .getSimpleName(), entity, field);
    Query q = em.createQuery(queryString);
    q.setParameter("entity", entity);
    return (T) q.getSingleResult();
}

@SuppressWarnings("unchecked")
public <T extends BasicEntity> T loadLazyFields(T entity, String[] fields) throws NoSuchFieldException {
    String queryString = String.format("SELECT x FROM %s x WHERE x = :entity", entity.getClass().getSimpleName());

    for (String field : fields) {
        if (!typeHasField(entity.getClass(), field)) {
            throw new NoSuchFieldException(entity.getClass().getSimpleName() + " has no field called " + field);
        }
        queryString += String.format(" LEFT JOIN FETCH x.%s", field);
    }
    Query q = em.createQuery(queryString);
    q.setParameter("entity", entity);
    return (T) q.getSingleResult();

}

private boolean typeHasField(Class<?> type, String field) {
    try {
        type.getDeclaredField(field);
        return true;
    } catch (NoSuchFieldException e) {
        return false;
    }
}
  • 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-23T01:11:14+00:00Added an answer on May 23, 2026 at 1:11 am

    You need a design pattern called “Open Session in View” keeping the persistence session open in the page rendering phase. This feature is provided by Integration Frameworks like Seam.

    I’m not sure if this is supported by JSF2, since many features from Seam2 moved to JavaEE6. Anyhow, you should take a look for Seam (although it’s hard to get something running with Seam3).

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

Sidebar

Related Questions

I have a web application that can load plugins through reflection. It currently uses
In a load test of our PHP based web application we can easily reach
How can I load a custom dll file in my web application? I've tried
I want to load test the web application that we're working on? Can you
I tried many times to get the connection between my web application and Derby
I have a web application that loads javascript on page load: $(function() { $('.modal-delete').click(function()
I am reading javascript web application and the author is using following code: mod.load
So it's easy to load balance an ASP.NET web application. You set up a
Is it possible to have a Java/J2EE web application integrate/communicate with SharePoint to load,
I am trying to load a google web toolkit application with django, but the

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.