CrudRepository的existsBy方法返回了错误的结果

6
我可以帮助您进行翻译。以下是翻译内容:

我遇到了以下问题:一个CrudRepository返回了一个错误的结果,与数据库中的数据不匹配。 有一个实体类:

@Entity
@Table(name = "ATTRIBUTE")
public class Attribute implements Serializable {

  private static final long serialVersionUID = 7360594743377794716L;
  *******

  @Id
  @Basic(optional = false)
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ID")
  private Long id;  
  *******
  @Column(name = "R_ORGANIZATION_ID")
  private Long refOrganizationId;  
  @Column(name = "TEXT_CODE")
  private String textCode;
  *******
  /**
  getters, settrs, hashCode, etc
  **/
}

我有一个适当的 CrudRepository

public interface AttributeRepository
    extends CrudRepository<Attribute, Long> {
  *****

  boolean existsByTextCodeAndRefOrganizationId(String textCode, Long organizationId);

  *****
}

问题在于仓库返回了不在数据库中的数据的true
例如,我将'asdf'作为textCode传递,将0作为organizationId。 尽管数据库中没有这样的textCode,但我仍然会得到true。以下是查询的自动生成代码:
    select
        TOP(?) attribute0_.id as col_0_0_ 
    from
        attribute attribute0_ 
    where
        attribute0_.text_code=? 
        and attribute0_.r_organization_id=?
12:00:24.805 [http-nio-8080-exec-6] TRACE o.h.t.d.s.BasicBinder - binding parameter [2] as [VARCHAR] - [asdf]
12:00:24.805 [http-nio-8080-exec-6] TRACE o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [0]

但是如果我在任何SQL管理器中运行查询,会得到0行。可能是什么原因呢?

相关信息:MS SQL Server 2012, Java 1.8, spring-boot.version 1.5.4.RELEASE


也许记录是在事务中创建并随后被删除或修改的? - talex
@talex 没有任何的创建/更新操作。只有选择。 - Yuriy Tsarkov
2
尝试将“exists...”替换为“get...”,并记录结果。这可能会给你一个关于它找到了什么数据的线索。 - talex
1个回答

8
深度调试显示问题在适当的服务方法调用序列中。主要问题是existsBy返回结果时会考虑到创建的实体,这些实体目前尚未在数据库中。因此,在我的问题之前的某些步骤中,Attribute实体已经被创建。这意味着Attribute已经在当前事务中创建,但尚未持久化,而existsBy已将其作为结果集的一部分处理。感谢大家!

检查事务隔离级别。 - Yan Khonski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接