gpt4 book ai didi

sql - 延迟加载的 primefaces 数据表在 SQL 语句完成执行之前得到更新

转载 作者:行者123 更新时间:2023-11-30 23:50:25 24 4
gpt4 key购买 nike

当 primefaces 数据表在用于更新\从该表中删除实体的 SQL 语句完成执行之前更新时,我遇到了问题。

01:08:13,255 INFO [reg.data.model.LazyEventDataModelMod]  - Loading the lazy event data between 0 and 10 
01:08:13,256 DEBUG [org.hibernate.SQL] -
select
count(*) as col_0_0_
from
events event0_ limit ?
01:08:13,394 DEBUG [org.hibernate.SQL] -
delete
from
events
where
id=?

问题是实体管理器没有返回任何信息来表明它已成功执行删除\更新查询。

我使用 JSF 2、Weld 1.1.5.Final、Primefaces 3.0.1、Seam persistence/transaction/solder 3.0.1.Final。
服务器是Tomcat 7。

下面是调用 Controller bean 上的操作的按钮的代码:

<p:commandButton id="deleteButton" value="#{msg.delete}"
icon="ui-icon-trash" update=":events_form :messages"
actionListener="#{eventController.delete()}" />

<p:commandButton id="refreshButton" value="#{msg.refresh}"
icon="ui-icon-refresh" update=":events_form :messages" />

更新不会在按下第一个按钮后发生,但是第二个按钮会更新表格,它们都有相同的 ajax 更新部分。

delete()方法代码:

public void delete() {
if (getSelEvent() != null) {
log.debug("deleting event " + getSelEvent().getId());

dao.delete(getSelEvent());

// Tried putting the code for reloading te data model here, but it is also executed before the sql statements...

} else {
log.warn("delete action cannot proceed, no event selected!");
}

}

dao 中的 deleteById() 方法:

public void delete(T entity) {
if (em == null)
init();
log.debugf("removing %s instance", simpleName);

entity = em.merge(entity);

// The problem lies here - em.remove() does not return any value to indicate success/failure, so the control is released before the SQL statements finish execution

em.remove(entity);
}

数据表:

<p:dataTable id="event_list" value="#{eventController.lazyModel}"
var="event" rowKey="#{event.id}" paginator="true" rows="20"
selection="#{eventController.selEvent}" selectionMode="single"
onRowSelectUpdate="content,messages"
onRowEditUpdate="content,messages"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,25,50,100">

LazyDataModel 类:

public class LazyEventDataModel extends LazyDataModel<Event> {

private static final long serialVersionUID = 2386224274507659474L;

private EventDAO dao;

private Logger log = Logger.getLogger(LazyEventDataModel.class);

private List<Event> data;

public LazyEventDataModel() {
dao = new EventDAO();
}

@Override
public Event getRowData(String rowKey) {
return dao.find(Long.parseLong(rowKey));
}

@Override
public Object getRowKey(Event event) {
return event.getId();
}

@Override
public List<Event> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {

if (sortField == null || "".equals(sortField)) {
sortField = "event_date";
sortOrder = SortOrder.DESCENDING;
}

if (sortField.equals("date"))
sortField = "event_date";

// Get the paginated and sorted data
data = dao.findPageObjects(Event.class, first, pageSize, sortField,
sortOrder, filters);

log.infof("Loading the lazy event data between " + first + " and "
+ (first + pageSize));

// Set the row count
this.setRowCount(Global.safeLongToInt(dao.count()));

return data;
}
}

我现在能想到的唯一解决方案是在调用 dao.delete(getSelEvent()) 之后添加计时器并在该计时器之后刷新数据模型。

最佳答案

命令按钮的更新属性也必须包含数据表。一旦 ajax 调用完成,更新中的任何设置都会发生。考虑到您没有创建单独的线程来执行数据库操作(这会使 ajax 请求几乎立即完成),请执行以下操作。

但有一个警告:出于某种原因,在更新属性中引用数据表的 ID 不起作用。我通常只是用 panelGroup 包装它,然后告诉按钮更新 panelGroup。像这样:

<p:commandButton value="Delete" actionListener="#{someBean.delete}" update="panel">
</p:commandButton>
<h:panelGroup id="panel">
<p:dataTable id="event_list" value="#{eventController.lazyModel}"
var="event" rowKey="#{event.id}" paginator="true" rows="20"
selection="#{eventController.selEvent}" selectionMode="single"
onRowSelectUpdate="content,messages"
onRowEditUpdate="content,messages"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="10,25,50,100">

...

</h:panelGroup>

请注意,primeFaces 按钮组件默认使用 ajax,因此您根本不需要 jsf2 ajax 标记。

关于sql - 延迟加载的 primefaces 数据表在 SQL 语句完成执行之前得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9198212/

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