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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:06:24+00:00 2026-06-15T16:06:24+00:00

I’m trying to write a query for my Grails application that selects all the

  • 0

I’m trying to write a query for my Grails application that selects all the instances of Apple that are “related” to an instance of Orange. Here the word “related” means all the instances of Banana that are associated with an instance of Apple are associated with some combination of instances of Cherry which are associated with the instance of Orange we are concerned with. I have looked at the question here, but my query is a little more complex and I’m not seeing how to apply the given answer to my problem.

Here are the classes I’m dealing with:

class Apple {
    static hasMany = [ bananas: Banana ]
}

class Banana {
}

class Cherry {
    static hasMany = [ bananas: Bannna ]
}

class Orange {
    static hasMany = [ cherries: Cherry ]
}

In picture form that looks like this:

enter image description here

In the scenario 1 shown below, the desired query would only return “Apple 1” because all the instances of Banana that are related to “Apple 2” are not related to “Orange 1” through some combination of instances of “Cherry”.

enter image description here

In the scenario 2 shown below, the desired query would return “Apple 1” and “Apple 2” because all the instances of Banana that are related to “Apple 2” are related to “Orange 1” through some combination of instances of “Cherry”.

enter image description here

Here is the query I have been working with:

Apple.executeQuery(
    "SELECT DISTINCT apples
    FROM Apple apples
    INNER JOIN apples.banannas banannas
    WHERE banannas IN(
        SELECT DISTINCT banannas
        FROM Cherry cherries
        INNER JOIN cherries.banannas banannas
        WHERE cherries IN(
            SELECT DISTINCT cherries
            FROM Orange orange
            INNER JOIN orange.cherries cherries
            WHERE orange =:myOrange
        )
    )
    ORDER BY apples.id ASC",
    myOrange: myOrange
)

The problem is that my query returns “Apple 1” and “Apple 2” for both scenario 1 and 2.

UPDATE #1:

As requested, here is the SQL generated by the HQL query. Sorry for the fruity obfuscation <<< pun

SELECT DISTINCT apple0_.id                     AS id10_,
                apple0_.version                AS version10_,
                apple0_.description            AS descript3_10_,
                apple0_.apple_priority_type_id AS apple4_10_,
                apple0_.apple_status_type_id   AS apple5_10_,
                apple0_.internal_need_date     AS internal6_10_,
                apple0_.name                   AS name10_
FROM   apple apple0_
       INNER JOIN apple_priority_type applepri1_
               ON apple0_.apple_priority_type_id = applepri1_.id
       INNER JOIN apple_status_type applesta2_
               ON apple0_.apple_status_type_id = applesta2_.id
       INNER JOIN apple_banana banana3_
               ON apple0_.id = banana3_.apple_bananas_id
       INNER JOIN banana banana4_
               ON banana3_.banana_id = banana4_.id
WHERE  banana4_.id IN (SELECT DISTINCT banana7_.id
                       FROM   cherry plum5_
                              INNER JOIN cherry_banana banana6_
                                      ON plum5_.id = banana6_.cherry_bananas_id
                              INNER JOIN banana banana7_
                                      ON banana6_.banana_id = banana7_.id
                       WHERE  plum5_.id IN (SELECT DISTINCT plum10_.id
                                            FROM   orange orange8_
                                                   INNER JOIN orange_cherry
                                                              plum9_
                                                           ON
orange8_.id = plum9_.orange_cherrys_id
INNER JOIN cherry plum10_
        ON
plum9_.cherry_id = plum10_.id
WHERE  orange8_.id = 248))
ORDER  BY apple0_.id ASC
LIMIT  100 

UPDATE #2:

I actually have a way to make this work with HQL, but it isn’t a nice one liner like I have in other places where I’m finding all of some type related to an instance of some other type. Here is my work around:

    def bananas= myOrange.cherries.bananas.flatten().unique()
    def apples = Apple.getAll().collectMany{ !it.bananas.isEmpty() && bananas.containsAll( it.bananas ) ? [ it ] : [] }.flatten().unique()
    namedParams.put( "apples", apples )
    if( !apples.isEmpty() ) {
        apples = Apple.executeQuery( "SELECT DISTINCT apples FROM Apple apples WHERE apples IN(:apples) ${additionalQuery} ORDER BY ${sortname} ${sortorder}", namedParams )
    }
    return apples
  • 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-15T16:06:25+00:00Added an answer on June 15, 2026 at 4:06 pm

    That’s because if your Apple have at least one relation with a banana, will always return.

    select
        distinct apple0_.id as id0_,
        apple0_.version as version0_ 
    from
        apple apple0_ 
    inner join
        apple_banana bananas1_ 
    where bananas1_.banana_id in (1,2,3)
    

    What you need to do is exclude from your results that apples where not exists a banana related with your Cherry. You will have to look if your database have a function like oracle minus.

    EDIT: This query removes from the list that apples that have a banana that’s not in the cherries bananas.

    Note: I not checked the performance of this.

      SELECT DISTINCT apples
        FROM Apple apples
        INNER JOIN apples.bananas bananas
        WHERE bananas IN(
            SELECT DISTINCT bananas
            FROM Cherry cherries
            INNER JOIN cherries.bananas bananas
            WHERE cherries IN(
                SELECT DISTINCT cherries
                FROM Orange orange
                INNER JOIN orange.cherries cherries
                WHERE orange =:myOrange
            )
        )
      and apples not in (
        SELECT DISTINCT apples
          FROM Apple apples
         INNER JOIN apples.bananas bananas
          WHERE bananas NOT IN(
            SELECT DISTINCT bananas
              FROM Cherry cherries
              INNER JOIN cherries.bananas bananas
              WHERE cherries IN(
                SELECT DISTINCT cherries
                FROM Orange orange
                INNER JOIN orange.cherries cherries
                WHERE orange = :myOrange
              )
          )
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.