Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate: No session after save of new entity
    primarykey
    data
    text
    <p>I create a new entity, store it the first time and then want to access the collections of the related classes: </p> <pre><code> @Override protected void onSubmit(final AjaxRequestTarget target, final Form&lt;?&gt; 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); } </code></pre> <p>I get the following error on the second line of logging:</p> <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: no session or session was closed </code></pre> <p>I have also tried </p> <pre><code>Hibernate.initialize(getDetailsModel().getObject().getField().getListOfSomething()); </code></pre> <p>This leads to a different error: </p> <pre><code>org.hibernate.HibernateException: collection is not associated with any session </code></pre> <p>This is not very surprising when debugging I can see that the collection proxies have no session associated with them. </p> <p>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:</p> <pre><code>@OneToMany(mappedBy = "field", fetch = FetchType.EAGER) private List&lt;E&gt; listOfSomething= new ArrayList&lt;E&gt;(); </code></pre> <p>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 <code>session.load(entity)</code> and a <code>session.merge(entity)</code> with no success.</p> <p>My entities look like this:</p> <pre><code>@Entity class A { @ManyToOne B b; } @Entity class B { @OneToMany(mappedBy = "b") List&lt;A&gt; aList; @OneToMany(mappedBy = "b") List&lt;C&gt; cList; } @Entity class C { @ManyToOne B b; } </code></pre> <p>What I am doing is creating a <code>c = new C()</code>, selecting <code>b</code> from a <code>DropDownChoice</code> and on submit I want to persist <code>getDao().save(c)</code>. 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 <code>c.getB().getAList()</code> and that is where the exception happens because the <code>b</code>I selected from the <code>DropdownChoice</code>is a detached entity and it's <code>aList</code> was not fetched and cannot be loaded lazily either.</p> <p>I cannot figure out how to get the still existing session into my new <code>C</code> instance. </p> <p>Here's more info from my <code>web.xml</code></p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;opensessioninview&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;opensessioninview&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload