gpc / grails-hibernate-filter Goto Github PK
View Code? Open in Web Editor NEWHome Page: http://grails.org/plugin/hibernate-filter
License: Apache License 2.0
Home Page: http://grails.org/plugin/hibernate-filter
License: Apache License 2.0
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:
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.
Plugin should be built and tests run on every commit
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.