Search code examples
mysqlspringhibernatespring-boothql

Word count in Hibernate Query Language (hql)


Currently I am using Hibernate Query Language (HQL). By using hql I want to create a function for getting the number of word in a function. I am not getting any clue from internet. So, guys please help me. Thank you in advance.

I want to create query like

SELECT post FROM Post post WHERE WORDCOUNT(post.details) > 99

Solution

  • I don't know of a way to do this using HQL alone. As a workaround, you can read each row into Java, and then manually count the number of words in details.

    If you are open to using a native query, then we can try counting words by counting the number of spaces in the details column. For example, on MySQL and Oracle, you could try the following query:

    SELECT post
    FROM post
    WHERE LENGTH(details) - LENGTH(REPLACE(details, ' ', '')) > 98;