Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate updating persistent records
    primarykey
    data
    text
    <p>We are using hibernate through the java based scripting language groovy.</p> <p>We retrieve record, update record and save the record.</p> <p>during update we clear the associations, and reload these before save.</p> <p>We get an exception when Hibernate flushes the record after associations are deleted but before new ones are established, triggering a validation failure.</p> <p>Ie we have a constraint on the associations: <code><pre>class Entity { ... static constraints = { association1(minSize:1) //require association to have at least 1 record } }</pre></code></p> <p><strong>What is the best approach to update a persistent record?</strong></p> <p>Some potential options:</p> <p>==============================================</p> <p>[1] create new record and copy over the properties:</p> <p><code><pre> def oldEntity = Entity.findByX(x) if (oldEntity) { oldEntity.properties = newEntity.properties oldEntity.save(flush:true);| } else { newEntity.save(flush:true); }</pre></code></p> <p>The concern here is this feels hacky - creating a detatched entity and copying over the properties over a live one.</p> <p>==============================================</p> <p>[2] retrieve record, update, save, with flush disabled:</p> <p><code><pre> sessionFactory.currentSession.flushMode = org.hibernate.FlushMode.MANUAL</p> <p>def existingEntity = Entity.findByX(x) existingEntity.association1.clear() existingEntity.association2.clear()</p> <p>existingEntity.association1.add(new Association(...)) existingEntity.association2.add(new Association(...))</p> <p>sessionFactory.currentSession.flushMode = org.hibernate.FlushMode.AUTO existingEntity.save(flush:true)</pre></code></p> <p>Not sure about this approach - don't like such interference with Hibernate's state management</p> <p>==============================================</p> <p>[3] Permanantly set flush mode to manual and use save() and save(flush:true) to persist records with cascade:"all-delete-orphan" to ensure associations are managed.</p> <p>The question here is this is not the default - and relies on us to manage Hibernate's flushing.</p> <p>==============================================</p> <p>Summary: </p> <p>All these approaches seem to smell - can anyone advise the least bad approach -or even best practice for this situation?</p> <p>Thanks</p> <p>alex</p>
    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.
    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