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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:38:42+00:00 2026-06-01T06:38:42+00:00

I create a new entity, store it the first time and then want to

  • 0

I create a new entity, store it the first time and then want to access the collections of the related classes:

 @Override
 protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {           
        final E entity = (E) form.getModelObject();
        getDao().save(entity); //calls session.saveOrUpdate(entity)
        LOG.debug("Saved entity " + entity);
        LOG.debug("Show collections " + entity.getField().getListOfSomething());
        parent.replaceContentPanel(parent.getDetailsPanel(parent.createReloadableModel(entity)), target);
 }

I get the following error on the second line of logging:

org.hibernate.LazyInitializationException: 
failed to lazily initialize a collection of role: 
no session or session was closed

I have also tried

Hibernate.initialize(getDetailsModel().getObject().getField().getListOfSomething());

This leads to a different error:

org.hibernate.HibernateException: collection is not associated with any session

This is not very surprising when debugging I can see that the collection proxies have no session associated with them.

I am using the ‘openSessionInView’ filter that comes with the Spring framework. The code works fine by the way when I want to update an existing entity. It also works when I set the fetchType to eager on the collection:

@OneToMany(mappedBy = "field", fetch = FetchType.EAGER)
private List<E> listOfSomething= new ArrayList<E>();

Do I really need to set this to EAGER? I want to avoid this very much and was hoping for a way around it. Is there a way to associate a newly stored entity with the Hibernate session? I have tried both a session.load(entity) and a session.merge(entity) with no success.

My entities look like this:

@Entity class A { 
    @ManyToOne B b;
}

@Entity class B {     
   @OneToMany(mappedBy = "b") List<A> aList;
   @OneToMany(mappedBy = "b") List<C> cList;
}

@Entity class C {
   @ManyToOne B b;
}

What I am doing is creating a c = new C(), selecting b from a DropDownChoice and on submit I want to persist getDao().save(c). After the submit I want to display my new entity on a different panel which is loaded via ajax. For example I want to display c.getB().getAList() and that is where the exception happens because the bI selected from the DropdownChoiceis a detached entity and it’s aList was not fetched and cannot be loaded lazily either.

I cannot figure out how to get the still existing session into my new C instance.

Here’s more info from my web.xml

<filter>
    <filter-name>opensessioninview</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>        
</filter>
<filter-mapping>
    <filter-name>opensessioninview</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
  • 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-01T06:38:43+00:00Added an answer on June 1, 2026 at 6:38 am

    I found a work-around that allows me to enforce loading the collections I know I will need with Hibernate.initialize(...)

    final ListModel<B> listModel = new ListModel<B>(bList);
    for (final B b : bList) {
       Hibernate.initialize(b.getAList());
    }
    add(new DropDownChoice<B>("b", listModel, new ChoiceRenderer<B>("name", "id")));
    

    When I add lines 2-4 from this snippet to my form all instances of B that I might later select for my new instance of C will have an initialized list of A.

    I am only half happy with this. I’d still prefer to be able to attach an object to the hibernate session on demand.

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

Sidebar

Related Questions

I create new desktop with CreateDesktop and want to get it's DC & RC.
First I want to refer to this post: Where to put Entity Framework Data
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I created a simple custom attribute on the sales/order entity. Now, for new orders,
I create new blank solution. Add exist web site. Structure: Solution E:...\projectname Folders: bin
I can't create New Project on my Visual Web Developer 2008 Express with SP1.
I tryed to create new components from one base Windows Form, also I found
I am trying to create new Event objects to be persisted in the database
I am tryıng to create new object of other class ın a for loop.
Eclipse and MyEclipse create new Java files with an extra blank line after 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.