I faced the following problem: A CrudRepository
returns a wrong result that is not matches database data.
There is an entity class:
@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
**/
}
And I have an appropriate CrudRepository
public interface AttributeRepository
extends CrudRepository<Attribute, Long> {
*****
boolean existsByTextCodeAndRefOrganizationId(String textCode, Long organizationId);
*****
}
The problem is that the repository returns true
for a data that is not in the DB.
For instance I am passing 'asdf' as a textCode
and 0 as an organizationId
. And I'm geting true
despite there is no such a textCode
in the DB.
Here is a autogenerated code of the query:
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]
But if I run a query in any SQL manager I will get 0 rows. What can it be?
Stuff: MS SQL Server 2012, Java 1.8, spring-boot.version 1.5.4.RELEASE
答案 0 :(得分:0)
深度调试向我展示了问题出在适当服务中方法的调用顺序。最主要的是,existsBy
返回的结果考虑了在existsBy
调用时不在数据库中的accout创建的实体。因此,在出现问题之前的某些步骤中,Attribute
实体已创建。这意味着Attribute
已在当前事务中创建,但尚未持久化,但是existsBy
已将其作为结果集的一部分。
谢谢大家!