0
I have the following java entity using spring:
public class ItemVendedor implements Serializable{
private static final long serialVersionUID = 1L;
private ItemVendedorPK id = new ItemVendedorPK();
private BigDecimal preco;
private Boolean disponivel;
private Date dt_insert;
private Date dt_update;
@EmbeddedId
public ItemVendedorPK getId() {
return id;
}
...GETs AND SETs
This entity is linked to a Jparepository class as below:
@Repository
public interface ItemVendedorRepository extends JpaRepository<ItemVendedor, ItemVendedorPK >, JpaSpecificationExecutor<ItemVendedor> {
}
what I want to create is a query that returns to me an object of type Itemvendedor, but should fetch the object of lower price.
Does the use of Keywords in Repository allow me to use some function similar to MIN() of SQL or JPQL? How could I do this using Keywords strategy?
I tried using @Query in JPQL in the respository, as below:
@Query("SELECT min(iv.preco) FROM ItemVendedor iv where (iv.id.produto.id = :produtoId) ")
Optional<ItemVendedor> findCestaFavorita( @Param(value = "produtoId") Long produto);
But in this approach always returns me the following error:
"message": "java.math.BigDecimal cannot be cast to com.eclodir.voucomprei.model.entity.ItemVendedor",
How to find the lowest priced product?
I believe the mistake of
@Query
is that you are creating a function likeOptional<ItemVendedor>
but in SQL returnsmin(iv.preco)
who’s kindBigDecimal
– Costamilam
It may be really, I’m forcing him to pull a single field, but I wanted the whole object, I’m going to do a test by putting the other fields to see the behavior.
– Gonzaga Neto