0

I'm trying to search for posts that have their strategy in the provided list, but every time it returns an empty list.

I thought that maybe some parameters were invalid, but debug mode shows that everything is just as it should be.

Then I tried using regular SQL from console to check if maybe the construction of HQL was invalid, but it returned all posts just fine.

I'm out of ideas; it must be something that I'm doing wrong with hibernate, but I can't tell what.

Method:

@Transactional
public List<Post> getPostsWithoutQuery(List<String> allowedStrategyList, int page, int resultsPerPage) {
    return entityManager.createQuery("from Post post where post.postStrategy in (:postStrategyList)", Post.class)
            .setParameter("postStrategyList", allowedStrategyList)
            .setFirstResult((page - 1) * resultsPerPage)
            .setMaxResults(resultsPerPage)
            .getResultList();
}

Debug values

Regular SQL:

select * from post p where p.post_strategy in ('ImagePostStrategy', 'VideoPostStrategy', 'StoryPostStrategy')

Regural SQL Result

Sky
  • 11
  • 1
  • 2
  • set hibernate.show_sql to true to see the generated query by your hql. Probably there is something wrong with the generated query. – Marwa Eldawy Nov 23 '19 at 17:18
  • also, I believe you should use SetParameterList instead of setParameter? – Marwa Eldawy Nov 23 '19 at 17:21
  • possibly, your question is a duplicate of: https://stackoverflow.com/questions/4828049/in-clause-in-hql-or-java-persistence-query-language – Marwa Eldawy Nov 23 '19 at 17:21
  • Hibernate sql: Hibernate: select post0_.id as id1_6_, post0_.create_date as create_d2_6_, post0_.description as descript3_6_, post0_.post_strategy as post_str4_6_, post0_.update_date as update_d5_6_ from post post0_ where post0_.post_strategy in (? , ? , ?) limit ? – Sky Nov 23 '19 at 17:24
  • In hibernate 5.4.9.Final there is not any method named setParemeterList – Sky Nov 23 '19 at 17:25

0 Answers0