gpt4 book ai didi

java - 在 Hibernate 中延迟初始化集合

转载 作者:行者123 更新时间:2023-12-01 06:22:35 25 4
gpt4 key购买 nike

在我的托管 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 条目时获取列表,只需在某些情况下)。

我使用的是 Hibernate,没有 JPA,这是上述每个类的 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>

仪表板页面项:

<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>

我该如何解决这个问题?

编辑:

这就是我检索仪表板的方式:

this.dashboard = dashboardService.trouver(idDashboard);

这将调用:

 public K get(int id) {
if(id == 0) return null;
return (K) getSessionFactory().getCurrentSession().get(getClasse(),id);
}

其中K仪表板

最佳答案

理想情况下,您不想将 hibernate 代理发送到 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)

因此,您将始终在事务上下文中处理您的实体(我建议在大多数情况下这样做)。

关于java - 在 Hibernate 中延迟初始化集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com