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
Deep debugging showed me thet the problem was in the sequence of invocations of methods in the appropriate service. The main thing is that existsBy
returns results taking into accout created entities that are not in the DB at the moment of the existsBy
invocations. So at some steps before my problem the Attribute
entity had been created. It means the Attribute
has been created in the current transaction and not has been persisted yet but existsBy
already has taken it as a part of the resultset.
Thank's everyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With