[Resolvido](JPA) Like de Integer

Olá, pessoas.

Busquei aqui no fórum, no google, mas não fui feliz.

Eu tenho na Entidade

private Integer numeracao;

e preciso fazer uma espécie de like onde, se eu buscar por “8” venha por exemplo “8”, “81”, “8888”, “84”, e todos os números que comecem com 8.

StringBuilder queryStr = new StringBuilder("SELECT e FROM Especificacao e ");
queryStr.append(" where e.numeracao like ?1");

Query query = em.createQuery(queryStr.toString);
query.setParameter(1, numeracao + "%")

List<Especificacao> resultado = query.getResultList();

Obviamente o código acima não funciona, eu ganho um java.lang.ClassCastException ao tentar executá-lo. A pergunta é, além de tranformar o campo em string, como eu poderia fazer isto?

[quote=CintiaDR]Olá, pessoas.

Busquei aqui no fórum, no google, mas não fui feliz.

Eu tenho na Entidade

private Integer numeracao;

e preciso fazer uma espécie de like onde, se eu buscar por “8” venha por exemplo “8”, “81”, “8888”, “84”, e todos os números que comecem com 8.

StringBuilder queryStr = new StringBuilder("SELECT e FROM Especificacao e ");
queryStr.append(" where e.numeracao like ?1");

Query query = em.createQuery(queryStr.toString);
query.setParameter(1, numeracao + "%")

List<Especificacao> resultado = query.getResultList();

Obviamente o código acima não funciona, eu ganho um java.lang.ClassCastException ao tentar executá-lo. A pergunta é, além de tranformar o campo em string, como eu poderia fazer isto? [/quote]

Corrigindo, não li ate o final, mal minha.

Tenta isso com JPAQL nunca tentei, mas pode te ajudar.
em sql existe a funcção cast, ve se hibernate te da suporte a ela.
CAST(Campo AS VARCHAR( 8 ) )

acho que vai te ajudar, na doc do hibernate:

"cast(… as …), where the second argument is the name of a Hibernate type, and extract(… from …) if ANSI cast() and extract() is supported by the underlying database "

Boa sorte.

Duplicado, mal minha.

Só vai funcionar se a coluna do seu banco for do tipo VARCHAR.
Like não funciona com colunas que sejam númericas.

[quote=Rapapel]

Tenta isso com JPAQL nunca tentei, mas pode te ajudar.
em sql existe a funcção cast, ve se hibernate te da suporte a ela.
CAST(Campo AS VARCHAR( 8 ) )

acho que vai te ajudar, na doc do hibernate:

"cast(… as …), where the second argument is the name of a Hibernate type, and extract(… from …) if ANSI cast() and extract() is supported by the underlying database "

Boa sorte.[/quote]

E não é funcionou bonitinho??? (Estou no Oracle 8i)


  queryStr += " where CAST(e.numeracao as string) like ?1";

Muito obrigada, Rapapel!

[quote=lucasap2005]Só vai funcionar se a coluna do seu banco for do tipo VARCHAR.
Like não funciona com colunas que sejam númericas.

[/quote]

Se eu trocasse pra String eu sabia que ia funcionar, eu precisava de outra solução! :slight_smile:

[quote=CintiaDR][quote=Rapapel]

Tenta isso com JPAQL nunca tentei, mas pode te ajudar.
em sql existe a funcção cast, ve se hibernate te da suporte a ela.
CAST(Campo AS VARCHAR( 8 ) )

acho que vai te ajudar, na doc do hibernate:

"cast(… as …), where the second argument is the name of a Hibernate type, and extract(… from …) if ANSI cast() and extract() is supported by the underlying database "

Boa sorte.[/quote]

E não é funcionou bonitinho??? (Estou no Oracle 8i)


  queryStr += " where CAST(e.numeracao as string) like ?1";

Muito obrigada, Rapapel! [/quote]

Que bom. O hibernate é fodão mesmo. rssss

Nem sei se vc ta usando hibernate, rsss. Ta usando hibernate mesmo ou toplink?

Boa sorte ai.