GithubHelp home page GithubHelp logo

gpc / grails-hibernate-filter Goto Github PK

View Code? Open in Web Editor NEW
10.0 10.0 37.0 1.51 MB

Home Page: http://grails.org/plugin/hibernate-filter

License: Apache License 2.0

Groovy 81.37% Batchfile 18.63%

grails-hibernate-filter's People

Contributors

ashneyderman avatar bendoerr avatar burtbeckwith avatar donalmurtagh avatar mdeimel avatar scottburch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

grails-hibernate-filter's Issues

Missing table aliases when applying filters to classes with a N-N relationship

Given these 2 domain classes with a many-many relationship between them:

class Condition {

    String name
    Integer status = 1

    static hasMany = [
            problems: Problem
    ]

    static hibernateFilters = {
        published(condition: "status = 1")
        published(collection: 'problems')
    }
}

class Problem {

    String name
    Integer status = 1

    static belongsTo = Condition

    static hasMany = [
            conditions: Condition
    ]

    static hibernateFilters = {
        published(condition: "status = 1")
        published(collection: 'conditions')
    }
}

When I execute this query

        List<Problem> problems = Problem.executeQuery("""
                SELECT          p
                FROM            ${Problem.simpleName} p
                JOIN            p.conditions as c
                WHERE           c.id = ?""", [condition.id])

The generated SQL Is

        select
            problem0_.id as id1_4_,
            problem0_.version as version2_4_,
            problem0_.name as name3_4_,
            problem0_.status as status4_4_ 
        from
            problem problem0_ 
        inner join
            condition_problems conditions1_ 
                on problem0_.id=conditions1_.problem_id 
                and status = 1 
        inner join
            condition condition2_ 
                on conditions1_.condition_id=condition2_.id 
        where
            status = 1 
            and condition2_.id=?

Notice that the status = 1 predicate which is not prefixed with a table alias. Because both the condition and problem tables have a column with this name, the query fails with error

Ambiguous column name "STATUS"; SQL statement:

Collection filters are applied to join table when classes have a N-N relationship

Given these domain classes

class Problem {

    String name
    Integer status = 1

    static belongsTo = Condition

    static hasMany = [
            conditions: Condition
    ]

    static hibernateFilters = {
        statusFilter(condition: "status = 1")
        collectionFilter(condition: 'status = 1', collection: 'conditions')
    }
}

class Condition {

    String name
    Integer status = 1

    static hasMany = [
            problems: Problem
    ]

    static hibernateFilters = {
        statusFilter(condition: 'status = 1')
        collectionFilter(condition: 'status = 1', collection: 'problems')
    }
}

If all the filters are enabled, and this query is executed

Problem.executeQuery("""
    SELECT  p
    FROM    ${Problem.simpleName} p
    JOIN    p.conditions""")

then the generated SQL is

        select
            problem0_.id as id1_4_,
            problem0_.version as version2_4_,
            problem0_.name as name3_4_,
            problem0_.status as status4_4_ 
        from
            problem problem0_ 
        inner join
            condition_problems conditions1_ 
                on problem0_.id=conditions1_.problem_id 
                and conditions1_.status = 1 
        inner join
            condition condition2_ 
                on conditions1_.condition_id=condition2_.id 
        where
            problem0_.status = 1

Notice in the SQL above, that the join table condition_problems is being filtered, rather than the associated collection (the condition table). The query naturally fails, because there is no status column in the join table.

This issue appears to be identical to this Hibernate bug which was opened almost 10 years ago against an early version of Hibernate 3

Although it's claimed that the linked Hibernate issue is a duplicate, I can't find any trace of the original, or any evidence that the problem is fixed in the Hibernate 4 version this plugin is using.

I've pushed a test to this PR that demonstrates the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.