gpt4 book ai didi

java - 实体保存在数据库中,但永远不会被获取

转载 作者:行者123 更新时间:2023-11-29 13:37:46 25 4
gpt4 key购买 nike

我们将 JBoss Seam 与 JPA 和 Hibernate 一起用作持久性提供程序。我们遇到了一个非常奇怪的问题。在我们的 UI 中,我们有一棵树。当我们向树中添加一个节点时,该节点会进入 postgres DB(行在那里,用 pgadmin 检查)。但是树没有改变。似乎获取节点及其子节点的命名查询(下面的 OrganizationEntity.FIND_BY_ISDELETED_MAIN)无法正常工作。新创建的对象应该出现在 OrganizationEntity.childOrganizations 下 - 但它没有。 FetchType.EAGER 没有帮助, session 刷新也没有帮助。

最可笑的是,大约半小时后,查询开始返回正确的结果!怎么会这样? hibernate 中是否有任何缓存需要清除或调整?

提前致谢!

UI 树:

<rich:tree id="OrgTrees" switchType="ajax" ignoreDupResponses="true">
<rich:recursiveTreeNodesAdaptor
roots="#{organizationServiceBean.getOrgTree()}" var="item"
nodes="#{item.childOrganizations}">
<rich:treeNode>
<a4j:commandLink value="#{item.fname}" ajaxSingle="true"
reRender="organizationViewForm,OrgTree"
onclick="this.disabled=true; if(g_flagChanges){if(confirm('Save changes?')){ updateCheck()} else{this.disabled=false;}}"
oncomplete="this.disabled=false; g_flagChanges=false;">
<f:setPropertyActionListener
target="#{organizationBean4.selectedOrganization}"
value="#{item}" />
<a4j:support event="onclick" />
</a4j:commandLink>

</rich:treeNode>
</rich:recursiveTreeNodesAdaptor>
</rich:tree>

OrganizationServiceBean 看起来像这样:

@Stateless
@Scope(ScopeType.EVENT)
@Name("organizationServiceBean")
public class OrganizationServiceBeanImpl implements OrganizationServiceBean, Serializable {
...
public List<OrganizationEntity> getOrgTree() {
return organizationService.findByIsDeletedMain(0);
}

}

organizationService 如下:

@Stateless
@AutoCreate
@Scope(ScopeType.STATELESS)
@Name("organizationService")
public class OrganizationServiceImpl extends
GenericServiceImpl<OrganizationEntity> implements OrganizationService {
……
public List<OrganizationEntity> findByIsDeletedMain(int isDeleted) {
//entityManager.flush();
List<OrganizationEntity> lOrg = findByCriteria(isDeleted, OrganizationEntity.FIND_BY_ISDELETED_MAIN);
log.info("search finished...");
return lOrg;
}
….
@SuppressWarnings("unchecked")
private List<OrganizationEntity> findByCriteria(Object searchKey,
String namedQuery) {

final Query query = entityManager.createNamedQuery(namedQuery);
query.setParameter("searchKey", searchKey);
List<OrganizationEntity> organizations = null;
try {
organizations = (List<OrganizationEntity>) query.getResultList();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return organizations;
}
…..
}

和组织实体:

@Entity
@Table(name = "organization", uniqueConstraints = { @UniqueConstraint(columnNames = "fname") })
@NamedQueries( {
….

@NamedQuery(name = OrganizationEntity.FIND_BY_ISDELETED_MAIN, query = "select org from OrganizationEntity org where org.isdeleted = :searchKey and org.parentOrganization is null order by org.id", hints={@QueryHint(name="org.hibernate.cacheable",value="false")}),
….})
@Name("organizationEntity")
public class OrganizationEntity extends AbstractEntity {

private static final long serialVersionUID = -9085028098941577562L;

….
public static final String FIND_BY_ISDELETED_MAIN = "OrganizationEntity.findByIsDeleted";
….
// bi-directional many-to-one association to Organization
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
private OrganizationEntity parentOrganization;

// bi-directional many-to-one association to Organization
@OneToMany(mappedBy = "parentOrganization", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@Where(clause="isdeleted = 0")
private List<OrganizationEntity> childOrganizations;
}

编辑

如果我们从 url 中删除 session ID,该功能就可以正常工作 - 这意味着

http://localhost:8080/h2p2/admin/admin.seam?cid=7

无论我们刷新多少次都不起作用,但是

http://localhost:8080/h2p2/admin/admin.seam

工作正常。

最佳答案

您可能有一个二级缓存缓存从查询中检索到的对象(查询本身被标记为 cacheable=false 但 hibernate 在实现对象时仍然可以查询缓存)。您可以在更新后(在可能缓存了该实体的每个服务器上)从二级缓存中逐出父对象,或者让查询中使用的实体管理器忽略缓存。

要执行后者,请在发出查询之前执行以下操作:

entityManager.setProperty("javax.persistence.cache.retrieveMode", CacheRetreiveMode.BYPASS);

这有效地禁用了查询该查询的缓存,并且可能是性能问题。

关于java - 实体保存在数据库中,但永远不会被获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781277/

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