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

Very stuck on a problem which I thought would be pretty simple. As an
I stuck with problem of mail application , i am developing a website for
im stuck with a problem im basically trying to create a function where I
I'm stuck with a problem and ask for your help. It's about navigation within
I am stuck with a problem when testing my controllers with RSpec - the
I got stuck in this problem for an hour. I am thinking this is
I'm stuck with a problem and hope stackoverfolw community can help me. I want
I got stuck with this problem. I wrap two tables inside of a h:form.
I am stuck with this problem for 3 days with no avail to solve
I'm stuck on a problem I really don't know how to solve: I have

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.