在我的托管bean中,我有如下代码:
FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> map = context.getExternalContext().getRequestParameterMap();
int pageIndex = Integer.valueOf(map.get("page"));
int pageItem = Integer.valueOf(map.get("pageItem"));
int widget = Integer.valueOf(map.get("widget"));
DashboardPageItem dashboardPageItem = new DashboardPageItem();
dashboardPageItem.setPosition(pageItem);
dashboardPageItem.setWidget(widgetService.trouver(widget));
for (DashboardPage dp : dashboard.getPages()) {
if (dp.getIndex() == pageIndex) {
dashboardPageItem.setDashboardPage(dp);
dp.getDashboardPageItems().add(dashboardPageItem);
}
}
但是当我运行它时,我收到此错误消息:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.dashboard.entity.DashboardPage.dashboardPageItems, no session or session was closed
因此,我有一个名为
Dashboard
的类,该类有一个DashboardPage
的列表,而每个DashboardPage
都有一个DashboardPageItem
的列表。(请注意,在获取DashboardPage条目时,我总是想获取DashboardPageItem的所有列表,对于DashboardPage列表,仅在某些情况下,我不需要每次调用Dashboard条目时都获得该列表)。
我正在使用没有JPA的Hibernate,这是上述每个类的hbm:
仪表板:
<class name="Dashboard" table="t_dashboard_das">
<id name="id" column="das_id">
<generator class="native"/>
</id>
<property name="name" column="das_name"/>
<property name="description" column="das_description"/>
<property name="visibility" column="das_visibility"/>
<many-to-one name="createur"
column="fk_sal_id"
foreign-key="sal_id"
not-null="false"
lazy="proxy"/>
<many-to-one name="role"
column="fk_rol_id"
foreign-key="rol_id"
not-null="false"
lazy="proxy"/>
<bag table="t_dashboard_page_dpa" name="pages" inverse="true" lazy="true" cascade="all">
<key column="fk_das_id" not-null="true" unique="false" foreign-key="das_id"/>
<one-to-many class="com.dashboard.entity.DashboardPage"></one-to-many>
</bag>
</class>
资讯主页:
<class name="DashboardPage" table="t_dashboard_page_dpa">
<id name="id" column="dpa_id">
<generator class="native"/>
</id>
<property name="name" column="dpa_name"/>
<property name="index" column="dpa_index"/>
<property name="model" column="dpa_model"/>
<many-to-one name="dashboard"
column="fk_das_id"
foreign-key="das_id"
not-null="false"
lazy="proxy"/>
<bag table="t_dashboard_page_item_dpi" name="dashboardPageItems" inverse="true" lazy="true" cascade="all">
<key column="fk_dpa_id" not-null="true" unique="false" foreign-key="dpa_id"/>
<one-to-many class="com.dashboard.entity.DashboardPageItem"></one-to-many>
</bag>
</class>
DashboardPageItem:
<class name="DashboardPageItem" table="t_dashboard_page_item_dpi">
<id name="id" column="dpi_id">
<generator class="native"/>
</id>
<property name="position" column="dpi_position"/>
<many-to-one name="dashboardPage"
column="fk_dpa_id"
foreign-key="dpa_id"
not-null="false"
lazy="proxy"/>
<many-to-one name="widget"
column="fk_wid_id"
foreign-key="wid_id"
not-null="false"
lazy="proxy"/>
</class>
我该如何解决?
编辑:
这就是我检索
Dashboard
的方式:this.dashboard = dashboardService.trouver(idDashboard);
这将调用:
public K get(int id) {
if(id == 0) return null;
return (K) getSessionFactory().getCurrentSession().get(getClasse(),id);
}
其中
K
是Dashboard
。 最佳答案
理想情况下,您不想将休眠代理发送到您的JSF托管Bean(将它们打包到DTO中进行处理)。
但是考虑到您当前的情况,我建议您创建一种新的服务方法来更新仪表板(首先它将合并仪表板以使其由持久性提供程序进行管理):
@Transactional
public void updateDashboard(Dashboard dashboard, DashboardPageItem dashboardPageItem){
Dashboard managedDashboard = session.merge(dashboard);
for (DashboardPage dp : managedDashboard .getPages()) {
DashboardPage managedDp = session.merge(dp);
if (managedDp.getIndex() == pageIndex) {
dashboardPageItem.setDashboardPage(managedDp);
managedDp.getDashboardPageItems().add(dashboardPageItem);
}
}
session.merge(managedDashboard);
}
然后从托管bean中调用:
dashboardService.updateDashboard(dashboard, dashobardPageItem)
因此,您将始终在事务上下文中处理实体(我建议在大多数情况下都应这样做)。