Skip to content

Commit

Permalink
fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Dónal Murtagh committed Mar 15, 2016
1 parent fb32f35 commit 8d90540
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion grails-app/domain/test/domain/college/Course.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Course {

static hibernateFilters = {
collegeFilter(condition: 'status = 1')
collegeFilter(collection: 'students')
collegeFilter(collection: 'students', joinTable: true)
}
}
7 changes: 7 additions & 0 deletions grails-app/domain/test/domain/college/Pen.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test.domain.college


class Pen {
String name
Integer status = 1
}
4 changes: 3 additions & 1 deletion grails-app/domain/test/domain/college/Student.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Student {

static hasMany = [
courses: Course,
loans: Loan
loans: Loan,
pens: Pen
]

static hibernateFilters = {
collegeFilter()
collegeFilter(collection: 'loans')
collegeFilter(collection: 'pens', joinTable: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import grails.util.Metadata
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.hibernate.cfg.Mappings
import org.hibernate.engine.spi.FilterDefinition
import org.hibernate.mapping.Collection
import org.hibernate.type.TypeFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -92,8 +93,15 @@ class HibernateFilterBuilder {
}
}

def filterArgs = [name, condition, true, null, null]

// now add the filter to the class or collection
entity.addFilter name, condition, true, null, null
if (entity instanceof Collection && options.joinTable) {
entity.addManyToManyFilter(*filterArgs)

} else {
entity.addFilter(*filterArgs)
}

// TODO: may be able to refactor this so that the factory creates the
// session with the filters rather than enabling them on each request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CollegeFilterSpec extends IntegrationSpec {
new Course(name: 'course')
.addToStudents(name: 'valid', status: 1)
.addToStudents(name: 'invalid', status: 0)
.save(failOnError: true)
.save(failOnError: true, flush: true)

sessionFactory.currentSession.clear()

Expand All @@ -57,11 +57,11 @@ class CollegeFilterSpec extends IntegrationSpec {
@Issue('https://github.com/burtbeckwith/grails-hibernate-filter/issues/9')
void 'query many-to-many relationship with HQL'() {

new Course(name: 'invalidCourse', status: 0).save(failOnError: true)
new Course(name: 'invalidCourse', status: 0).save(failOnError: true, flush: true)
new Course(name: 'validCourse')
.addToStudents(name: 'valid', status: 1)
.addToStudents(name: 'invalid', status: 0)
.save(failOnError: true)
.save(failOnError: true, flush: true)

sessionFactory.currentSession.clear()

Expand All @@ -77,6 +77,23 @@ class CollegeFilterSpec extends IntegrationSpec {
courses[0].students.size() == 1
}

@Issue('https://github.com/burtbeckwith/grails-hibernate-filter/issues/9')
void 'query unidirectional one-to-many relationship'() {

new Student(name: 'Mark')
.addToPens(name: 'valid pen', status: 1)
.addToPens(name: 'invalid pen', status: 0)
.save(failOnError: true, flush: true)

sessionFactory.currentSession.clear()

when:
def student = Student.findByName('Mark')

then:
student.pens.size() == 1
}

void 'query one-to-many relationship with HQL'() {

new Student(name: 'invalid', status: 0).save(failOnError: true)
Expand Down

0 comments on commit 8d90540

Please sign in to comment.