Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:22+00:00 2026-05-28T23:33:22+00:00

I was trying to write a search query in hibernate. I took help from

  • 0

I was trying to write a search query in hibernate. I took help from open-meeting source code.
So my query:

public User getAllUser(String search) {
    String[] searchItems = search.split(" ");
    try {
        String sqlQuery = "SELECT * FROM  USER u " + "WHERE u.deleted = 1 ";
        sqlQuery += "AND ( ";
        for (int i = 0; i < searchItems.length; i++) {
            if (i != 0) {
                sqlQuery += " OR ";
            }
            sqlQuery += "( " + "lower(u.last_name) LIKE '"
                    + StringUtils.lowerCase("%" + searchItems[i] + "%")
                    + "' " + "OR lower(u.first_name) LIKE '"
                    + StringUtils.lowerCase("%" + searchItems[i] + "%")
                    + "' " + "OR lower(u.username) LIKE '"
                    + StringUtils.lowerCase("%" + searchItems[i] + "%")
                    + "' " + ") ";
        }
        sqlQuery += " )";

        log.debug("sqlQuery : " + sqlQuery);
        Query query = sessionFactory.getCurrentSession().createQuery(
                sqlQuery);

        return (User) query.list().get(0);

    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return null;
}

It says there is an unexpected token, I but I didn’t find where it is.

So I have two questions:

  1. how to make it functional.
  2. how to write search query using Criteria and Projection/Restrictions in hibernate
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:33:23+00:00Added an answer on May 28, 2026 at 11:33 pm

    This is not a hibernate query. It is a native SQl. You should use createSQLQuery as below

    Query query = sessionFactory.getCurrentSession().createSqlQuery(
                    sqlQuery);
    

    Refer this for select using hibernate.

    Also, I would suggest you to go with Criterias

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write search function, that gets its parameter from the user and
I'm trying to write a query for an advanced search page on my document
I'm trying to write an SQL query that would search within a CSV (or
I'm trying to write a query for the find-as-you-type search bar. What I want
I am trying to write a query that will search for orders that contain
I'm trying to write a query that joins a handful of tables from MySQL
I am trying to write a query for a search. I have a meta
Hi i need a query help (and yes i have been trying to search
I am trying to write a query that will search a list of strings
I'm pretty new to MYSQL. I'm trying to write a query which will search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.