实体保存在DB中,但永远不会被获取

时间:2011-01-24 11:10:33

标签: java hibernate postgresql jpa seam

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

最荒谬的是,大约半小时后查询开始返回正确的结果!怎么会发生?是否需要清除或调整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;
    }
…..
}

和OrganizationEntity:

@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中删除会话ID,那么功能就可以正常工作 - 这意味着

http://localhost:8080/h2p2/admin/admin.seam?cid=7
无论我们刷新多少次,

都不起作用,但

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

工作正常。

1 个答案:

答案 0 :(得分:1)

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

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

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

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