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

  • Home
  • SEARCH
  • 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 6201643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:35:32+00:00 2026-05-24T04:35:32+00:00

I fear that I miss a very basic point, but I’m stuck at the

  • 0

I fear that I miss a very basic point, but I’m stuck at the moment, hopefully someone can point my eyes starring to hard at it to the right one…
I try to go through my application setup step by step for make myself clear (hoping people won’t get bored before they reach the end of the posting):

I have a session scoped CDI component serving as a handler for a JSF 2.0 view. It holds an entity manager, a list of objects and a special single object:

@Named
@SessionScoped
public class EventHandler implements Serializable {

  @PersistenceContext
  private EntityManager em;

  private List<MyEvent> events;

  private MyEvent currentEvent;
  ...

When the view requests the list for displaying a table, it fills the list by querying the database:

Query query = em.createQuery("select e from MyEvent e");
events = (ArrayList<MyEvent>) query.getResultList();

The view shows this in a data table and provides a link to an action method within the handler:

<h:dataTable value="#{eventHandler.events}" var="_var">
  ...
    <h:commandLink action="#{eventHandler.linkAction(_var)}"> ... </h:commandLink>

The action method stores the reference:

public void setCurrentEvent(MyEvent currentEvent) {
  this.currentEvent = currentEvent;
  ...

Within the entity used in the collection and references above, there’a relationship which gets loaded lazily by default:

@ManyToMany(mappedBy="supportedServices")
public Set<MyEntity> getSupportingEntities() {
  ...

Now, when being in the detail view and trying to access this relationship by doing:

#{eventHandler.currentEvent.supportingEntities...}

I get a org.hibernate.LazyInitializationException telling me that the session is closed.

Shouldn’t the handler be able to load the requested relationship at a later point in the 2nd view, when the handler is session scoped and loaded the references via a query?

  • 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-24T04:35:33+00:00Added an answer on May 24, 2026 at 4:35 am

    Hum, I think this is because the persistenceContext, event if it is a member of a session scoped bean, is not session scoped.
    When the view is called the first time, a persistence context is created, the entities(events) are loaded and the datatable is populated. Then the persistence context is flushed, closed and the response is sent to the client. This all happen in the same http request.

    But when you then call the details view, another http request is issued, and another persistence context is created, and your reference to “currentEvent” is no more attached to the last persistence context. Hence the LazyInitializationError (the entity must be managed to lazy load).

    Solutions could be :

    • Use an extended persistence context (explained here).
    • Eager fetch the relation :

      @ManyToMany(mappedBy="supportedServices", fetch=FetchType.EAGER)

    • Merge the detached instance of currentEvent before using it in the details view :

      entityManager.merge(currentEvent);

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

Sidebar

Related Questions

I want to develop a GAE application using python, but I fear that Google
I have seen a codebase recently that I fear is violating alignment constraints. I've
I fear this is probably a bit of a dummy question, but it has
I fear that some of my code is causing memory leaks, and I'm not
Ok, I fear that this is just me having forgotten some small stupid thing
I come from a Java/Eclipse background and I fear that I am spoiled by
I have an application that must be prepared to work in various companies. But
I always try to avoid to return string literals, because I fear they aren't
I'm creating my first class, mainly guided by Overland's C++ Without Fear. I've made
If I release the object that's holding a reference to the variable that I

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.