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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:21+00:00 2026-06-05T05:38:21+00:00

Let’s say I have two JPA entities: @Entity public class EntityA { @Id private

  • 0

Let’s say I have two JPA entities:

@Entity
public class EntityA {
  @Id
  private String id;

  @OneToMany
  private List<EntityB> b;

  ...
}

@Entity
public class EntityB {
  @Id
  private String id;

  ...
}

What I would like to accomplish is to define a @NamedQuery that retrieves all EntityB instances that are not referenced from any EntityA.

Naively, I would have tried

@NamedQuery(name = "EntityB.findAllUnassigned", query = "SELECT b FROM EntityB b WHERE NOT b IN (SELECT DISTINCT a.b FROM EntityA a)")

but this ends up generating invalid SQL:

select b0_.id as id1_, b0_.attr, ... from b b0_ where b0_.id not in  (select distinct . from a a1_, a_b ab2_, b b3_ where a1_.id=ab2_.a_id and ab2_.b_id=b3_.id)

Any alternatives?

  • 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-06-05T05:38:22+00:00Added an answer on June 5, 2026 at 5:38 am

    Weird that this query does not give you an exception. But your b.a reference is not legal in this way; you cannot select the collection itself, you want/need to select the collection elements.

    SELECT b FROM EntityB b WHERE b NOT IN (SELECT DISTINCT elements(a.b) FROM EntityA a)
    

    Note however that elements is a Hibernate HQL construct, not JPA.

    If you care about JPA portability:

    SELECT b FROM EntityB b WHERE b NOT IN (SELECT DISTINCT b2 FROM EntityA a join a.b b2)
    

    * NOT IN is the more normal form, so I used that here as well

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

Sidebar

Related Questions

Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have two Entity classes: SocialApp and SocialAppType In SocialApp I have
Let's say we have the following: abstract class A; class B : public A;
Let's say I have a base class 'A' and two classes 'B' and 'C',
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say we have 3 classes: A, B and C. Each class has the
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have one class User, and it has a property of type

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.