Seemingly Random 1=1 where clause generated by GORM's where closure

≡放荡痞女 提交于 2019-12-25 05:24:07

问题


Sample Program With Problem: https://github.com/HybridProgrammer/GormOneIsOneError

Problematic GORM Code

def query = UserData.where {
        (
                teams { id in me.getAuthorities().id } && status { isOpen == true }
        ) || (
                owner == me && status { isOpen == true }
        )
    }

Summary of the Problem

The generated SQL query occasionally produces a where clause containing 1=1 when I would expect to see teams_alia1_.id in (?)

Detailed Description of the Problem

I wrote two integration test to illustrate this problem:

Please see src/intrgration-test/groovy/ExampleITSpec.groovy

Integration Test 1

void "never fails - direct approach"() {
    given:
    setupData()
    def me = User.first()

    when:
    def query = UserData.where {
        (
                teams { id in me.getAuthorities().id } && status { isOpen == true }
        ) || (
                owner == me && status { isOpen == true }
        )
    }

    then:
    me.getAuthorities().size() == 1
    query.size() == 2
}

Always generates SQL

select count(*) as y0_ 
from user_data this_ inner join status status_ali2_ 
     on this_.status_id=status_ali2_.id 
     inner join workflow_role_teams teams5_ 
     on this_.id=teams5_.user_data_teams_id 
     inner join role teams_alia1_ 
     on teams5_.role_id=teams_alia1_.id 
where 
(((teams_alia1_.id in (?)) and (status_ali2_.is_open=?)) 
or 
(this_.owner_id=? and (status_ali2_.is_open=?))) limit ?

Integration Test 2

void "sometimes fails"() {
    given:
    setupData()
    def me = User.first()

    when:
    def query = exampleService.getMyOrMyTeamsData(me)

    then:
    me.getAuthorities().size() == 1
    query.size() == 2
}

Occasionally generates SQL

select count(*) as y0_ 
from user_data this_ inner join status status_ali2_ 
    on this_.status_id=status_ali2_.id 
    inner join workflow_role_teams teams5_ 
    on this_.id=teams5_.user_data_teams_id 
    inner join role teams_alia1_ 
    on teams5_.role_id=teams_alia1_.id 
where 
((1=1 and (status_ali2_.is_open=?)) 
or 
(this_.owner_id=? and (status_ali2_.is_open=?))) limit ?

Where does the 1=1 come from?

ExampleService

def getMyOrMyTeamsData(User me) {
    // To fix the test Toggle these two lines
    def user = me
//        def user = User.get(me.id)

    def query = UserData.where {
        (
                teams { id in me.getAuthorities().id } && status { isOpen == true }
        ) || (
                owner == user && status { isOpen == true }
        )
    }

    return query

}

Update 1

While debugging the application you can clearly see authorities variable is passed along to DetachedCriteria#handleJunction(Closure callable) in the working version but vanishes when called from the service method.

Integration Test 1 - Has authorities attribute

Integration Test 2 - No authorities attribute

Solution

Please see solution branch https://github.com/HybridProgrammer/GormOneIsOneError/commit/8c3d77f82081d0367961345b4fc70c789e322318


回答1:


For me the test always fails and I don't know why test 2 and 3 show different behavior, but after I changed List teams to List<Role> teams in UserData.groovy, test 3 passed.

In the GORM docs the declaration of mapped fields is often omited, which also works in this case.

Hope this also works for you.



来源:https://stackoverflow.com/questions/43331616/seemingly-random-1-1-where-clause-generated-by-gorms-where-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!