Home > other >  JPA Cast BigDecimal as string
JPA Cast BigDecimal as string

Time:12-23

I'm using JPA to query a database, but something strange happens. In some cases the cast works and compiles fine, but with a BigDecimal it doesn't let me use the cast to make a LIKE with a String. Here is a part of the code that works:

"AND cast(pe.pesoObjetivo as string) LIKE :pesoObjetivo% "

pesoObjetivo is a data type Double

@Column(name = "peso_objetivo")
    private Double pesoObjetivo;

But when I try to perform the query with

"AND cast(pe.pesoPregestacional) LIKE :pesoPregestacional% "  

pesoPregestacional being a BigDecimal

 @Column(name = "peso_pregestacional", precision = 21, scale = 2)
    private BigDecimal pesoPregestacional;

I get the following error

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ) near line 1, column 701

And the rest of my query How could I, inside JPA, do a LIKE operation of the BigDecimal data with a String? Or what is my mistake? By commenting out this line of code or by using

"AND pe.pesoPregestacional = :pesoPregestacional% "  

Everything works correctly, it's just not what I want. So clearly the error is in this sentence

CodePudding user response:

You missed as string:

Wrong

"AND cast(pe.pesoPregestacional) LIKE :pesoPregestacional% "  

Correct

"AND cast(pe.pesoPregestacional as string) LIKE :pesoPregestacional% "  
  • Related