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 7524683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:07:59+00:00 2026-05-30T03:07:59+00:00

I’m trying to construct a complex query. My entities look as follows: @Entity public

  • 0

I’m trying to construct a complex query. My entities look as follows:

@Entity
public class Configuration{

   @Id
   @Column(name="CONF_ID")
   protected Long configurationId;

   @ManyToMany
   @MapKey(name="componentType")
   @JoinTable(name="CONF_COMP",
      joinColumns={@JoinColumn(name="CONF_ID", referencedColumnName="CONF_ID")},
      inverseJoinColumns={@JoinColumn(name="COMP_ID", referencedColumnName="componentId")})
   protected Map<String, Component> components;
}

And

@Entity
public class Component {

    @Id
    protected long componentId; 
    @ElementCollection
    protected Map<String, String> properties;

    @ManyToMany(mappedBy="components")
    private List<Configuration> configurations;

    @Column(name="COMP_TYPE")
    protected String componentType;
 }

My problem lies in querying the properties field properly. I can’t seem to create a query to get all Configurations where Component A has Property Prop1 = 1 and Component B has Property Prop2=2.
I tried the following without success.

Root<Configuration> conf = cq.from(Configuration.class);    
MapJoin<Configuration, String, Component> compJoin = conf.join(Configuration_.components, JoinType.LEFT);
MapJoin<Component, String, String> propJoin = compJoin.join(Component_.properties, JoinType.LEFT); 

    Predicate p1 = cb.and(
                cb.equal(mapJoin.key(), "A"),
                cb.equal(propJoin.key(), "Prop1"), cb.equal(propJoin.value(), "1"));
    Predicate p2 = cb.and(
                cb.equal(mapJoin.key(), "B"),
                cb.equal(propJoin.key(), "Prop2"), cb.equal(propJoin.value(), "2"));

Predicate[] pArray = new Predicate[]{p1, p2};
cq.where(pArray);
cq.select(conf).distinct(true);

EDIT: The query, as outputted by the logger, looks like this:

SELECT DISTINCT  t2.CONF_ID, t2.DTYPE, t2.TOTALPRICE, t2.NAME
 FROM CONFIGURATION t2 
 LEFT OUTER JOIN (CONF_COMP t3 JOIN COMPONENT t1 ON (t1.COMPONENTID = t3.COMP_ID)) ON (t3.CONF_ID = t2.CONF_ID) LEFT OUTER JOIN Component_PROPERTIES t0 ON (t0.Component_COMPONENTID = t1.COMPONENTID) 
WHERE (((((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?) AND (((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?)) AND (t2.DTYPE = ?))
bind => [7 parameters bound]

I’m guessing it is trying to find a Configuration where all the conditions apply to the SAME component maybe? It works if I apply restrictions on only one Component, but I get an empty result list when applying 2 or more, although there are entries in the DB that satisfy the conditions.

UPDATE

After following Pimgd’s suggestion, i ended up with a query that looks like this:

SELECT DISTINCT t1.CONF_ID, t1.DTYPE, t1.TOTALPRICE, t1.NAME
FROM CONFIGURATION t1 LEFT OUTER JOIN (CONF_COMP t2 JOIN COMPONENT t0 ON (t0.COMPONENTID = t2.COMP_ID)) ON (t2.CONF_ID = t1.CONF_ID) 
WHERE ((( 
    t0.COMPONENTID IN (SELECT t3.COMPONENTID 
                        FROM COMPONENT t3 LEFT OUTER JOIN Component_PROPERTIES t4 ON (t4.Component_COMPONENTID = t3.COMPONENTID) 
                        WHERE ((t4.PROPERTIES_KEY = Brand) AND (t4.PROPERTIES = Intel)))) 

    AND 
    t0.COMPONENTID IN (SELECT t6.COMPONENTID 
                        FROM COMPONENT t6 LEFT OUTER JOIN Component_PROPERTIES t7 ON (t7.Component_COMPONENTID = t6.COMPONENTID) 
                        WHERE ((t7.PROPERTIES_KEY = Capacity) AND t7.PROPERTIES LIKE 4GB%))))

One criteria works, two yield no results.

Any help much appreciated!

  • 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-30T03:08:00+00:00Added an answer on May 30, 2026 at 3:08 am

    I’m gonna say that
    mapJoin.key() should be compJoin.key().

    Other than that, I don’t see anything wrong.

    If my offered solution doesn’t work, here are some bonus questions for you:

    • What are the results you are getting?
    • Is it possible to show the generated queries?

    An interesting bit I spotted:
    WHERE (((((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?) AND (((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?)) AND (t2.DTYPE = ?))

    What if, I take this bit out… just for clarification. ((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?))

    WHERE (((SNIP AND t0.PROPERTIES LIKE ?) AND (SNIP AND t0.PROPERTIES LIKE ?)) AND (t2.DTYPE = ?))

    So yeah…
    Now how would you go about fixing your query?
    Sadly, I am no expert.
    But what I do know is that if you made a query with a where going WHERE t2.CONF_ID IN (Subselect for criteria A) AND t2.CONF_ID IN (Subselect for criteria B).

    My suggestion would be to look up subselects and abuse those. JPA 2.0, Criteria API, Subqueries, In Expressions explains it and seems relevant enough.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.