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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:16:19+00:00 2026-06-05T18:16:19+00:00

First of all, I use Java EE, Hibernate with EntityManager and PrimeFaces. I have

  • 0

First of all, I use Java EE, Hibernate with EntityManager and PrimeFaces.

I have one EJB module (business logic and domain) and two WAR modules (Jersey WS and JSF PrimeFaces).

I decided to initialize lazy collections in JSF WAR module to avoid lazy initialization exception. I don’t use extended entity manager.

@ManagedBean(name = "company")
@SessionScoped
public class CompanyBean {

    @EJB
    private CompanyFacade cf;

    ...

    public String showDetails(Long id) {
        company = cf.find(id);
        Hibernate.initialize(company.getCompanyTypes());
        Hibernate.initialize(company.getPrimaryUser());
        Hibernate.initialize(company.getBlocked());
        Hibernate.initialize(company.getAddresses());
        Hibernate.initialize(company.getContacts());
        return "DETAILS";
    }

    ...
}

And I get:


Caused by: org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:474)
at org.hibernate.Hibernate.initialize(Hibernate.java:417)
at minepackage.CompanyBean.showDetails(CompanyBean.java:79)
...

I don’t understand it. There has to be a session when one line before the initialization it was fetched from database, doesn’t it? I initialize attributes in WS module in similar way and there it’s working.

Any idea what’s happening?

  • 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-06-05T18:16:22+00:00Added an answer on June 5, 2026 at 6:16 pm

    I think the session is closed after your EJB finished, so the objects are in detached state. So Hibernate.initialize() won’t work any more. You have multiple options here:

    • Open the transaction on the client side (in your JSF bean or in a servlet filter). This way the session will still be open when your are calling Hibernate.initialize().
    • Modify your EJB to load the full object and all the required collections. You could use fetch joins and/or use Hibernate.initialize() there.
    • Create a more fine grained API in your EJB. Method like CompanyFacade.getAddressesByCompany().

    I would prefer a combination of the latter two. Use fetch joins to load the one-to-one and many-to-one relationships in your find method and add extra methods for loading the one-to-many collections (like addresses). This will also improve performance of your backend because it reduces the number of database queries.

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

Sidebar

Related Questions

First of all: I want to use Java EE not Spring! I have some
When I first started reading about Python, all of the tutorials have you use
First of all, this is the first time that I use Hibernate so my
first of all, i'm a noob at java programing for android. Now, I have
First of all: I use GlassFish 3.1 + Eclipse Java EE indigo. I want
First of all, I have to use and create this DOM element in a
I trying to learn how to use Java Swing's GroupLayout. First of all I
First of all, I don't want to use a join because that will make
I use camel case which has the first letter of all variable names, functions,
If model-first, we use [MetadataType(typeof(ConceptMetadataSource))] to attach a MetadataSource file which contains all 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.