2
I am using the Namedquery of JPA, to create a DTO. And in my Query I have as condition one IN and as a parameter of IN has values with white spaces in the middle. (Ex: 9292 929). and these values with white space, returns nothing. Only values without whitespace.
@Query("SELECT NEW br.com.dto.LancamantoDto(m.id, r.id) \n" +
"FROM Lancamento l \n" +
"WHERE l.seuNumero IN (:seuNumero) \n" +
"AND l.data = :data")
List<LancamentoDto> findAllBySeuNumeroAndData(@Param("seuNumero")List<String> seuNumero, @Param("data")LocalDate data);
Olá Danilo.
Lancamento.seuNumero
is aString
? If it is aNumber
(Integer
,BitInteger
, etc) you will need to pass a list of the respective type. E.g.,List<Integer> seuNumero
.– Anthony Accioly
Hello Anthony, at the moment I call the method, I pass a List<String> but within this List, it has values that have white spaces in the middle, and it is necessary that white spaces in the number.(Ex: 8283E 3434) when I need to search only one Lancamento.seuNumer I have to use LIKE in Query, with wildcards % in the whitespace, but for collection I don’t know what I need to do.
– Danilo Fernandes
Hello Danilo, my question is about the type of
Lancamento.seuNumero
, what will determine how this list is interpreted is the type ofl.seuNumero
. That’s aString
in your pojo mapped?– Anthony Accioly
So @Anthony the
Lancamento.seuNumero
is String yes.– Danilo Fernandes
And in the database that value is a
VARCHAR
? If yes and both strings are equal, it should be possible to make a query with=
orIN
hassle-free.– Anthony Accioly
A simple check. The name in SELECT is
Lancamanto
, with Same? It may just be a typo, but it doesn’t cost to validate.– Fellipe Soares
@Anthonyaccioly yes, in the database is varchar, and if I perform this query directly in the database (SQL Server) works normal with whitespaces.
– Danilo Fernandes
Danilo. Very, very strange even, mainly the fact of simple consultations with
=
does not work. It seems that your values are being converted or that your encoding is getting lost... already tried to create some entries in whichseuNumero
is equal to a handful of characters (e. g.ç$%€
) and check if the consultation works on the Iberian side?– Anthony Accioly
Maybe Spring Data is doing something weird too. Try turning your query into a
@NamedQuery
and use the standard Hibernate API (e. g.,session.getNamedQuery
).– Anthony Accioly
I did the tests, putting one with special characters, and he found normal, via Namedquery, and the default Hibernate API. But I realized something when the value of
seuNumero
, has only 1 blank space( e.g.127999 00 00 00001
) it returns me the record, but when I try to pass aseuNumero
with more than 1 blank space (e. g.128384 00 00 00001
) he ignores thisseuNumero
. Very strange this.– Danilo Fernandes
Interesting. Even by default API? Who knows if you copy the database combination to the code
128384 00 00 00001
, I’ve seen cases of characters that look like spaces but aren’t.– Anthony Accioly