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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:27:09+00:00 2026-05-19T14:27:09+00:00

I’m trying to put together a complicated query using Hibernate. I’ve been leaning toward

  • 0

I’m trying to put together a complicated query using Hibernate. I’ve been leaning toward Criteria, but I’m beginning to suspect it’s not possible, and so any suggestions would be helpful.

I have an entity structure like the following:

public class Attribute {
    private Integer id;
    private String name;
    private Set<Value> values;
}

public class Instance {
    private Integer id;
    private int instanceRef;
    private Set<Value> values;
}

public class Value {
    private Integer id;
    private Attribute attribute;
    private String localAttributeName;
    private Instance instance;
    private String value;
}

These entities are related as you’d expect:

value.attribute_id --> attribute.id
value.instance_id --> instance.id

Now, I would like to be able to take a set of attribute/value pairs (Strings) and find all instances that contain all of them. In Value, only one of attribute and localAttributeName are non-null, so the attribute name may match either localAttributeName or attribute.name. And to complicate things one last time, the unique index on Value is on (instance, attribute, value) or (instance, localAttributeName, value) — that is, within an Instance, any given Attribute may have multiple Values.

This is what I have so far:

public List<Instance> getMatchingInstances(Map<String, String> attrValues) {
    Criteria crit = session.createCriteria(Instance.class, "i");
    for(Map.Entry<String, String> entry : attrValues) {
        DetachedCriteria valueCrit = DetachedCriteria.forClass(Value.class, "v");

        // Do something here with valueCrit

        crit.add(Subqueries.exists(valueCrit));
    }
    return crit.list();
}

Based on the research I’ve done, what I’ve tried for that Do something section is:

    // This would only check localAttributeName and not attribute.name.
    // That's okay -- once I get the rest to work, I can figure this out.
    valueCrit.add(Restrictions.eq("localAttributeName", entry.getKey());
    valueCrit.add(Restrictions.eq("value", entry.getValue());
    valueCrit.add(Restrictions.eqProperty("v.instance_id", "i.id"));

But this throws the exception below, which I suspect is telling me I can’t do this with Criteria, but I’d love to learn otherwise:

java.lang.NullPointerException
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:341)

What would be the best way to go about doing this?

  • 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-19T14:27:10+00:00Added an answer on May 19, 2026 at 2:27 pm

    I figured out the solution after a few hours of banging on it. Hopefully, this is of use to others. There were three main points that I needed to solve to make this feasible:

    1. Add a Projection
    2. Create the proper joins
    3. Properly map the subquery back to the main criteria

    I’ve highlighted each of these in the below code.

    First, to get rid of the exception, I discovered that the subquery needed a projection, highlighted below. I just did a projection on the “id” property of Instance.

    Second, to get the join, I used the Criteria.createCriteria() methods to create a left outer join. Because I had multiple conditions at different levels of the join, I had to save the joined Criteria and attach expressions to them separately. This let me do my OR expression in the subquery.

    Finally, I had to add an eqProperty() clause to map the subquery back to the main Criteria. Just like it would need to be in the resulting SQL, I used: instance.id = i.id. Because I had already mapped the Instance Criteria to “i” and was adding this clause to the Value Criteria, this translated to the SQL: v.instance_id = i.id.

    Here’s the working code:

    public List<Instance> getMatchingInstances(Map<String, String> attrValues) {
        Criteria crit = session.createCriteria(Instance.class, "i");
        for(Map.Entry<String, String> entry : attrValues) {
            String attrName = entry.getKey();
            String val = entry.getValue();
    
            // Create the subquery
            DetachedCriteria valueCrit = DetachedCriteria.forClass(Value.class, "v");
    
            // Join the Attribute object (left outer join)
            DetachedCriteria attrCrit = 
              valueCrit.createCriteria("attribute", CriteriaSpecification.LEFT_JOIN);
    
            // Put together the OR statement on the Attribute joined criterion.
            Criterion localAttr = Restrictions.eq("v.localAttributeName", attrName);
            Criterion globalAttr = Restrictions.eq("name", attrName);
            attrCrit.add(Restrictions.or(localAttr, globalAttr));
    
            // Simple column equality on the subquery criterion.
            valueCrit.add(Restrictions.eq("value", val));
    
            // Map the subquery back to the outer query.
            valueCrit.add(Restrictions.eqProperty("instance.id", "i.id"));
    
            // Add the missing projection.
            valueCrit.setProjection(Projections.property("id"));
    
            // Add this subquery to the outer query.
            crit.add(Subqueries.exists(valueCrit));
        }
        return crit.list();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.