Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails dynamic finder where field name contains reserved words
    primarykey
    data
    text
    <p>So I have a grails domain class:</p> <pre><code>class Message { Inbox inbox Boolean hasBeenLogicallyDeletedByRecipient ... static belongsTo = [ inbox:Inbox, ... ] static constraints = { hasBeenLogicallyDeletedByRecipient(nullable:false) ... } } </code></pre> <p>I would like to use a dynamic finder as follows:</p> <pre><code>def messages = Message.findAllByInboxAndHasBeenLogicallyDeletedByRecipient( inbox, false, [order:'desc',sort:'dateCreated']) </code></pre> <p>This works fine running a unit test case in STS 2.6.0.M1 against grails 1.2.1; Spinning up the web app, it fails because of the <em>By</em> in <em>hasBeenLogicallyDeletedByRecipient</em> (I'm guessing it has confused the dynamic finder parsing when building up the query).</p> <p>I can use a criteria builder which works in the app:</p> <pre><code> def messages = Message.withCriteria { and { eq('inbox', inbox) eq('hasBeenLogicallyDeletedByRecipient', false) } order('dateCreated', 'desc') } </code></pre> <p>But since <code>withCriteria</code> is not mocked, it doesn't immediately work in unit tests, so I could add the following to the unit test:</p> <pre><code> Message.metaClass.static.withCriteria = { Closure c -&gt; ... } </code></pre> <p>Is the criteria/unit test mocking the best/accepted approach? I don't feel completely comfortable with mocking this, as it sidesteps testing the criteria closure. </p> <p>Ideally, I'd rather use the dynamic finder - is there a succinct way to make it work as is? If there is no way around it, I suppose the field name <em>could</em> be changed (there is a reason why I don't want to do this, but this is irrelevant to the question)...</p> <p><strong>UPDATE:</strong></p> <p>Here's the stacktrace when I try to use <code>findAllByInboxAndHasBeenLogicallyDeletedByRecipient()</code> inside the app - notice how it appears to get the last <em>By</em> and treat everything else between it and <em>findAll</em> as a property. i grazed on <a href="http://grails.org/OperatorNamesInDynamicMethods" rel="nofollow">http://grails.org/OperatorNamesInDynamicMethods</a> but it doesn't mention anything about <em>By</em> being verboten.</p> <pre><code>org.codehaus.groovy.grails.exceptions.InvalidPropertyException: No property found for name [byInboxAndHasBeenLogicallyDeleted] for class [class xxx.Message] at xxx.messages.yyyController$_closure3.doCall(xxx.messages.yyyController:53) at xxx.messages.yyyController$_closure3.doCall(xxx.messages.yyyController) at java.lang.Thread.run(Thread.java:662) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload