Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Grails is using Hibernate in the background. Typically you don't update properties inside the <code>PreUpdate</code> event. You can do it, but if you don't update both the property and the underlying <code>PersistentEntity</code> you will probably end up with some undesired behavior.</p> <p>The workflow which explains why you're getting two updates:</p> <ol> <li>You make changes to your domain object, and the time comes to persist the event.</li> <li>Hibernate decides what to persist.</li> <li>BeforeUpdate fires, updates <code>lat</code>/<code>lng</code> properties.</li> <li>Hibernate persists the object, but with the old values for <code>lat</code>/<code>lng</code>.</li> <li>Domain object is now <code>dirty</code></li> <li>Hibernate decides it needs to persist the dirty object (now with the correct values).</li> <li>BeforeUpdate fires, gets same value for <code>lat</code>/<code>lng</code> from API.</li> <li>Hibernate persists the object with the new values.</li> <li>The domain object and the persistent object now match. Hibernate is happy.</li> </ol> <p><strong>The simplest solution to avoid a double update would be to use the <code>SaveOrUpdate</code> event instead of <code>BeforeUpdate</code> because that actually happens earlier in the process.</strong></p> <p><strong>However,</strong> since your goal is to minimize the number of unnecessary API calls it's worth still using <code>BeforeUpdate</code> because manually checkig your entity for dirtiness before calling the API is probably more effort than it's worth. So, what you have to do is, as @Andrew suggested, just make sure you update the property <em>and</em> the entity, i.e.:</p> <pre><code>event.entity.setProperty('lat', coords.results.geometry.location.lat[0]) event.entity.setProperty('lng', coords.results.geometry.location.lng[0]) cli.lat = coords.results.geometry.location.lat[0] cli.lng = coords.results.geometry.location.lng[0] </code></pre> <p>Update: My assumption that checking whether the domain object is dirty might be annoying is actually based on code I had to write for NHibernate. This being Grails of course, it should be as simple as this:</p> <pre><code>if (event.entityObject.isDirty()) { /* Call API */ } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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