S
S
Sergey Ozeransky2015-08-29 18:46:18
Java
Sergey Ozeransky, 2015-08-29 18:46:18

Spring Data Jpa @Query error?

I have two entities:

public class DiscountsEntity implements Serializable {
    private Integer id;
    private Integer discount;

    @ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name = "id_item", nullable = false)
    private ItemEntity id_item;
....

public class ItemEntity implements Serializable {
    private Integer id;
    @NotNull
    private Integer price;
    @NotEmpty
    private String name;

    @OneToMany
    @JoinColumn(name="id")
    private List<ItemEntity> id_item;
...

In DiscountsEntityRepository I write an arbitrary query for the selection:
public interface DiscountsEntityRepository extends JpaRepository<DiscountsEntity, Integer> {
    //@Query("select d.discount from DiscountsEntity d where d.id_item = :id")
    //DiscountsEntity findById_Item(@Param("id") Integer id);
    //@Query("select d from DiscountsEntity d where d.id_item = :item")
    //DiscountsEntity findById_item(@Param("item") ItemEntity item);
    //@QueryHints(value = {@QueryHint(name = "name", value = "value")})
    //Page<DiscountsEntity> findById_item(ItemEntity id_item);
    //DiscountsEntity findById_item_Id(Integer id_item_Id);
    //List<DiscountsEntity> findById_item(ItemEntity id_item);
    //@Query("select d.discount from DiscountsEntity d where d.id_item = ?1")
    //List<DiscountsEntity> findById_item(Integer id_item);
    @Query("select d from DiscountsEntity d where d.id_item = :id_item")
    DiscountsEntity findById_item_Id(Integer id_item);
}

everything that is commented out there is an attempt to make a request that did not lead to anything except the same or other errors.
but I get an error:
HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1

I can't figure out the problem, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2015-08-29
@Div100

Probably because id_item is not Integer.
private ItemEntity id_item;
findById_item_Id(Integer id_item);
try

@Query("select d from DiscountsEntity d where d.id = :id_item")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question