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

  • SEARCH
  • Home
  • 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 6623241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:35+00:00 2026-05-25T21:30:35+00:00

I have a query that I have been trying to turn into a CriteraQuery,

  • 0

I have a query that I have been trying to turn into a CriteraQuery, but I don’t get how to code the “IN” term of the query.

The JPA Query that works looks like this:

@NamedQuery(name = "User.fromSearchID",
    query = "SELECT q FROM User q,"
    + " IN (q.data) AS s WHERE s.data LIKE :search"
    + " ORDER BY q.id")

And the entity that it works on looks like this:

@Entity
public class User {

    @Id private Integer id;

    @OneToMany private List<UserData> data;

    ... }

And the referenced entity is

@Entity
public class UserData {

    @Id private Long id;

    private String key;

    private String data;

    ... }

The intended result (the NamedQuery works) is to pull out all User entities that have some match in their list of data attributes. For some reason the CriteraQuery syntax escapes my intuition. Any help?

  • 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-25T21:30:35+00:00Added an answer on May 25, 2026 at 9:30 pm

    I assume users should be unique?
    Then JP QL version without rather old fashion IN is:

    String searchArgument = "data1";
    String query = " SELECT DISTINCT(u) " +
                   " FROM User u JOIN  u.data ud " +
                   " WHERE ud.data LIKE :search ORDER BY u.id";
    List<User> result =em.createQuery(query, User.class).
                    setParameter("search", searchArgument).getResultList();
    

    And same via Criteria API:

    String searchArgument = "data1";
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> user = cq.from(User.class);
    
    Predicate dataLike = cb.like(user.join("data").<String>get("data"),
                                 cb.parameter(String.class, "search"));
    cq.distinct(true).select(user)
        .where(dataLike)
        .orderBy(cb.asc(user.get("id")));
    
    TypedQuery<User> findUniqueUserByUserDataData = em.createQuery(cq);
    findUniqueUserByUserDataData.setParameter("search", searchArgument);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that i've been trying to figure out how to convert
I have the following MySQL query that I'm trying to translate into an equivalent
I have been trying to add data from my ajax query that returns json
I have a working sql query that I have been trying to convert to
I am trying to get a list of records that have been in the
In my example code below, you can see that I have been trying to
I have an query that has been giving me trouble. I think there is
I am using in C# MYsql .I have query that works if I run
I have a query that I know can be done using a subselect, but
I have a working query that retrieves the data that I need, but unfortunately

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.