Open monetschemist opened 6 years ago
It very hard to follow the above description. I would request you to please explain the problem in a very simple way.
Not sure how to explain the problem more simply, but I will try.
The Animals domain class describes an animal with its name, date of birth and parents (sire = father, dam = mother) which are also animals, so the domain class is self referential.
For example, given the data set up in the sample appliation, I can execute the following code
def animal = Animals.findByName('Society Lady') // find the animal whose name is Society Lady
assert animal.name == 'Society Lady' // verify the animal's name
assert animal.sire.name == 'Mr. Prospector' // verify the animal's father's name
assert animal.dam.name == 'La Voyageuse' // verify the animal's mother's name
assert animal.dam.sire.name == 'Tentam' // verify the animal's mother's father's name (maternal grandfather)
I can filter the Animals domain class using DetachedCriteria and I can combine or chain several DetachedCriteria together to make more elaborate filters. The reason I am taking this approach is that I have a browser front-end for Animals that provides column-level filtering using JQuery and DataTables, which works well with a set of chained DetachedCriteria on the back end.
Generally this filtering approach works EXCEPT if I try to filter first by mother's name, then by maternal grandfather's name.
Following the example above, if I filter first by animal.dam.name = 'La Voyageuse' , then by animal.dam.sire.name == 'Tentam', I expect to have the animal whose name is Society Lady returned. Instead, nothing is returned.
If I reverse the order of the filtering, that is I filter first by animal.dam.sire.name == 'Tentam', then by animal.dam.name = 'La Voyageuse' , I get the result I expect - the animal whose name is Society Lady is returned.
Does this seem clear enough?
Hmm maybe not. Here's attempt number 2 at greater simplicity.
In the linked application:
Two combined DetachedCriteria of the form
criteria = criteria.build {
sire {
ilike 'name','Mr. Prospector'
}
}
criteria = criteria.build {
dam {
sire {
ilike 'name','Tentam'
}
}
}
should therefore return the record for 'Society Lady', but they don't return anything.
If their order is reversed, they correctly return the record for 'Society Lady'.
(also pardon my ignorance but I don't know whether I can / should untag "Awaiting Feedback" or whether that's up to @puneetbehl).
@monetschemist I am in the middle of completing some other times. I will come back to this as soon as I am done with those.
@puneetbehl I see you've removed the status: in progress, does this mean the problem is fixed (sorry for the dumb question)
@puneetbehl wondering if there's anything I can do to contribute to solving this problem?
I see you've removed the status: in progress, does this mean the problem is fixed
No. It means no one is actively working on the issue right now. Once it is fixed the issue would be closed.
Brief description: detached criteria used to filter instances of a self-referencing class deliver incorrect results in specific cases documented below.
Steps to Reproduce
I have created a simple Grails application with one self-referencing domain class, populated by BootStrap.groovy, that demonstrates the problem.
This application should be cleaned and compiled. There is a default CRUD controller and views but these aren't necessary for the demonstration of the problem.
Once the application is built, here are the specific steps that demonstrate the problem:
In order to ensure all is working, use the Grails console to execute the following commands:
This will list all the animals currently loaded in the database. The result should appear as:
In that list we can see the animal "Society Lady" whose sire is "Mr. Prospector", dam is "La Voyageuse" and maternal grandsire is "Tentam".
To demonstrate that a single detached criteria works as expected, use the Grails console to execute the following commands:
As expected, this will report
To demonstrate that two chained detached criteria work as expected, use the Grails console to execute the following commands:
As expected, this will report
To demonstrate the case where the two chained detached criteria fail to work correctly, use the Grails console to execute the following commands:
Unexpectedly, no rows satisfy the chained criteria - the animal Society Lady is not reported.
Surprisingly (to me anyway), by reversing the order of the criteria in the above test to first select sire of dam name, then sire name:
The expected answer is once again reported.
Expected Behaviour
See above: all tests with the detached criteria, whether singly or in pairs, should yield this line of output:
Actual Behaviour
See above: one test, where the first criteria refers first to sire.name and the second to dam.sire.name fails to deliver that result. However if the order of the criteria are reversed, the correct answer is generated.
Environment Information
Example Application
https://github.com/monetschemist/grails-detached-criteria-problem.git