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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:09:13+00:00 2026-05-16T15:09:13+00:00

I am stuck with a problem concerning JPA-2.0 queries with relationships. How would it

  • 0

I am stuck with a problem concerning JPA-2.0 queries with relationships. How would it be possible to select any Dataset with at least one Event with type = B?

@Entity
class Dataset {
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "dataset")
    public List<Event> events;
}

@Entity
class Event {
    @ManyToOne
    @JoinColumn
    public Dataset dataset;

    public Type type;
}

enum Type {
     A, B, C
}

My starting point is

CriteriaBuilder _builder = em.getCriteriaBuilder();
CriteriaQuery<Dataset> _criteria = _builder.createQuery(Dataset.class);

// select from
Root<Dataset> _root = _criteria.from(Dataset.class);
_criteria.select(_root);

// apply some filter as where-clause (visitor)
getFilter().apply(
   _root, _criteria, _builder, em.getMetamodel()
);

// how to add a clause as defined before?
...

Any ideas on this. I tried to create a subqueries as well as a join, but I somehow did it wrong and always got all datasets as result.

  • 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-16T15:09:14+00:00Added an answer on May 16, 2026 at 3:09 pm

    Try

    SELECT d FROM DataSet d WHERE EXISTS 
        (SELECT e FROM Event e WHERE e.dataSet = d and e.type = :type)
    

    EDIT: As Pascal pointed out it looks like you are using the Criteria API. Not as familiar with this, but I’ll have a stab.

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Dataset> criteria = builder.createQuery(Dataset.class);
    
    Root<Dataset> root = criteria.from(Dataset.class);
    criteria.select(root);
    
    // build the subquery
    SubQuery<Event> subQuery = criteria.subQuery(Event.class);
    Root<Event> eventRoot = subQuery.from(Event.class);
    subQuery.select(eventRoot);
    
    ParameterExpression<String> typeParameter = builder.parameter(String.class);
    Predicate typePredicate = builder.equal(eventRoot.get(Event_.type), typeParameter));
    
    // i have not tried this before but I assume this will correlate the subquery with the parent root entity
    Predicate correlatePredicate = builder.equal(eventRoot.get(Event_.dataSet), root);
    subQuery.where(builder.and(typePredicate, correlatePredicate);
    
    criteria.where(builder.exists(subQuery)));
    
    List<DataSet> dataSets = em.createQuery(criteria).getResultList();
    

    Phew that was hard work. I’m going back to Linq now.

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

Sidebar

Related Questions

I am stuck on this problem which I am studying for an exam tomorrow.
Im working on an ASP.Net app with c#. I am stuck on a problem
I'm stuck on a RegEx problem that's seemingly very simple and yet I can't
I'm stuck on a PHP 4 server, and I would like to start moving
I am having a problem converting one of my company's scripts from csh to
This is similar to the cutting stock problem , but with a slight difference.
Being stuck with a legacy database schema that no longer reflects your data model
Really stuck on trying to write code to unzip a file or directory on
Disclaimer: I'm stuck on TFS and I hate it. My source control structure looks
I'm a bit stuck on this. Basically I want to do something like the

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.