Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating (without executing) a detached Criteria (with a .where{..}) can cause select count(*) queries to run #1460

Open
3 tasks done
tircnf opened this issue May 25, 2021 · 0 comments

Comments

@tircnf
Copy link

tircnf commented May 25, 2021

Task List

  • Steps to reproduce provided
  • [NA] Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. create a where query that joins in a related class:

  2. Don't execute the where query.

@Entity
class Pet {
    static hasMany = [nickNames: NickName]
    String name
}

@Entity
class NickName {
    static belongsTo = [pet: Pet]
    String nickname
}

def query = Pet.where {
            def tags = nickNames
            tags.nickname == "Spot"
            order("tags.nickname")
}

Expected Behaviour

No sql statements should be executed.

Actual Behaviour

A count(*) query is executed on the relationship table.
select count(*) as y0_ from nick_name this_ limit ?

Environment Information

  • Operating System: windows
  • GORM Version: 7.0.8
  • Grails Version (if using Grails): 4.0.10
  • JDK Version: 8

Example Application

https://github.com/tircnf/ExtraQuery

The only source code in the project is the following hibernateSpec which requires that sql logging be enabled in logback.groovy.

logger 'org.hibernate.SQL', DEBUG
import grails.persistence.Entity
import grails.test.hibernate.HibernateSpec
import org.junit.Rule
import org.springframework.boot.test.rule.OutputCapture

class ExtraQuerySpec extends HibernateSpec {

    @Rule
    OutputCapture capture = new OutputCapture()


    @Override
    List<Class> getDomainClasses() {
        return [Pet, NickName]
    }


    void testSetup() {
        expect:
        true
        !capture.toString()
    }

    void testLogging() {
        expect:
        new Pet(name: "jerry").save(flush: true, failOnError: true)
        capture.toString().contains("insert into pet")
    }

    void testCriteria() {
        when: "I create a detachedCriteria"
        Pet.where {

        }
        then: "no query is executed"
        !capture.toString()
    }

    void "test and query join criteria."() {
        when: "I create query with join and projection"
        Pet.where {
            def tags = nickNames
            tags.nickname == "Spot"

            order("tags.nickname")

        }

        then: "No query should have been executed, but count(*) from nick_name runs."
        !capture.toString()
    }
}

@Entity
class Pet {

    static hasMany = [nickNames: NickName]
    String name

}

@Entity
class NickName {
    static belongsTo = [pet: Pet]
    String nickname
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant