You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nested criteria querying works incorrectly with mock domain objects #14617
In my real application code, the ID value is a custom value object which is always assigned by the app. The ID corresponds to the URI of the resource in my SPARQL datastore.
I ran into this problem unit testing my app, so I created a new grails app (linked above) to reproduce the issue. Initially, I could not repro the issue in the new grails app.
I spent some time walking through the new grails app and my real app side by side in the debugger. I saw the map query framework used for the mock domain objects was doing some things that didn't entirely make sense to me. It seemed to be mixing and matching IDs from the different types of domain objects into a single query result list. I wasn't sure if this was correct behavior or not, but I noticed that the map query framework does a lot of ID comparison when executing queries.
In my unit test in the new app, there was only 1 instance of each domain object type, so all of the mocked domain objects involved in my test had a default ID of 1.
I figured that this might be masking a bug in the framework, because of the ID comparison being done in the query. Sure enough, when I set the IDs to different values explicitly, the unit test I was looking at in the new test app stopped working.
I don't have a thorough understanding the simple map query framework... but I am pretty sure that the current behavior is not correct. At the very least, if I am using a query syntax which is known not to be supported by simple map, it should throw an exception.
I think you may have misdiagnosed some of the behavior you are seeing. The ids don't all have to be the same in order for the query to work. You shouldn't be assigning values to the ids and I expect that is relevant. If you remove that code, your test will pass. You can verify that the ids being the same is not the issue by creating different numbers of instances of your domain classes in your setup method so that the ones you are testing with all have different ids (create and persist 7 Parent, 5 Child and 3 GrandChild, for example).
@jeffbrown If you run the app using run-app, you can see that the app is handling the query correctly. The data defined in Bootstrap is identical to the data defined in the test environment, including the ID assignment. The unit test behavior is inconsistent with the behavior of the in-memory DB.
I don't understand why you are saying that data persisted with assigned IDs is not valid.
I don't understand why you are saying that data persisted with assigned IDs is not valid.
It isn't valid to assign values to the id property unless the id generator is set to assigned. I haven't debugged to find out why that is causing a problem for your test, but it is an invalid thing to do.
Activity
jeffscottbrown commentedon Apr 18, 2017
https://github.com/rmorrise/nested-query-mock-test/blob/d511e26e585fdca6f56c52a3dfbbd406228c854a/src/test/groovy/nested/query/mock/test/TestControllerSpec.groovy#L20-L32
Why are you assigning values to the
idproperties in your test?rmorrise commentedon Apr 18, 2017
@jeffbrown Good question.
In my real application code, the ID value is a custom value object which is always assigned by the app. The ID corresponds to the URI of the resource in my SPARQL datastore.
I ran into this problem unit testing my app, so I created a new grails app (linked above) to reproduce the issue. Initially, I could not repro the issue in the new grails app.
I spent some time walking through the new grails app and my real app side by side in the debugger. I saw the map query framework used for the mock domain objects was doing some things that didn't entirely make sense to me. It seemed to be mixing and matching IDs from the different types of domain objects into a single query result list. I wasn't sure if this was correct behavior or not, but I noticed that the map query framework does a lot of ID comparison when executing queries.
In my unit test in the new app, there was only 1 instance of each domain object type, so all of the mocked domain objects involved in my test had a default ID of 1.
I figured that this might be masking a bug in the framework, because of the ID comparison being done in the query. Sure enough, when I set the IDs to different values explicitly, the unit test I was looking at in the new test app stopped working.
I don't have a thorough understanding the simple map query framework... but I am pretty sure that the current behavior is not correct. At the very least, if I am using a query syntax which is known not to be supported by simple map, it should throw an exception.
jeffscottbrown commentedon Apr 18, 2017
I think you may have misdiagnosed some of the behavior you are seeing. The ids don't all have to be the same in order for the query to work. You shouldn't be assigning values to the ids and I expect that is relevant. If you remove that code, your test will pass. You can verify that the ids being the same is not the issue by creating different numbers of instances of your domain classes in your
setupmethod so that the ones you are testing with all have different ids (create and persist 7 Parent, 5 Child and 3 GrandChild, for example).jeffscottbrown commentedon Apr 18, 2017
As far as I can tell, the query syntax you are using is fine and does appear to work when the data is valid.
rmorrise commentedon Apr 18, 2017
@jeffbrown If you run the app using run-app, you can see that the app is handling the query correctly. The data defined in Bootstrap is identical to the data defined in the test environment, including the ID assignment. The unit test behavior is inconsistent with the behavior of the in-memory DB.
I don't understand why you are saying that data persisted with assigned IDs is not valid.
jeffscottbrown commentedon Apr 19, 2017
It isn't valid to assign values to the id property unless the id generator is set to
assigned. I haven't debugged to find out why that is causing a problem for your test, but it is an invalid thing to do.jeffscottbrown commentedon Apr 19, 2017
Not only is it an invalid thing to do, there is no reason to do it.
borinquenkid commentedon Nov 15, 2026
Hibernate 5 and in-memory part in #17009 (stacked on #16931); the Hibernate 7 part follows on 8.0.x.
borinquenkid commentedon Nov 15, 2026
Hibernate 7 part in #17010 (stacked on #16930).
borinquenkid commentedon Nov 15, 2026
Neo4j part in #17011 (stacked on #16836).