的創建一個新的實體,存儲它的第一次,然後希望訪問相關的類的集合:休眠:無會話後保存新的實體
@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);
}
我上第二以下錯誤日誌的路線:
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role:
no session or session was closed
我也曾嘗試
Hibernate.initialize(getDetailsModel().getObject().getField().getListOfSomething());
這導致了不同的錯誤:
org.hibernate.HibernateException: collection is not associated with any session
這並不是很令人驚訝,當調試我可以看到集合代理沒有與他們關聯的會話。
我正在使用Spring框架附帶的'openSessionInView'過濾器。當我想要更新現有的實體時,代碼可以正常工作。當我將fetchType設置爲渴望集合時,它也可以工作:
@OneToMany(mappedBy = "field", fetch = FetchType.EAGER)
private List<E> listOfSomething= new ArrayList<E>();
我真的需要將它設置爲EAGER嗎?我非常想避免這種情況,希望能有辦法解決這個問題。有沒有辦法將新存儲的實體與Hibernate會話關聯起來?我已經嘗試了session.load(entity)
和session.merge(entity)
都沒有成功。
我的實體是這樣的:
@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;
}
什麼我做的是創造一個c = new C()
,從DropDownChoice
選擇b
並提交我要堅持getDao().save(c)
。提交之後,我想在通過ajax加載的不同面板上顯示我的新實體。例如,我想顯示c.getB().getAList()
,這是發生異常的原因,因爲從DropdownChoice
中選擇的b
是分離的實體,並且它的aList
未被提取,也無法延遲加載。
我無法弄清楚如何將仍然存在的會話放入我的新C
實例中。
下面是從我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>
您正在使用的'openSessionInView'過濾器,事務何時/何地啓動並提交? – rotsch 2012-04-03 07:26:19
你的實體是怎樣的?例如。保存操作是否會級聯到集合? – bert 2012-04-03 08:32:55
請包含web.xml的相關部分(調度程序servlet和OpenSessionInView過濾器) – pap 2012-04-03 14:00:55