Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is more related to Java than anything else. You can't change a runtime type of an instance (at runtime), so hibernate does not provide for this kind of scenarios (unlike rails for example).</p> <p>If you want to evict the user from the session, you'll have to evict associated entities. You have some choices:</p> <ul> <li>Map the entity with evict=cascade (see the <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#evict(java.lang.Object)" rel="nofollow noreferrer">Session#evict</a> javadoc)</li> <li>Manually evict all associated entites (this can be cumbersome)</li> <li>Clear the session, thus evicting all entities (of course, you'll lose the local session cache)</li> </ul> <p>I've had a similar problem in grails, and my solution is similar to <strong>grigory's</strong> solution. I copy all instance fields, including the associated entities, then delete the old entity and write the new one. If you don't have that many relations this can be easy, but if your data model is complex you'll be better off using the native sql solution you described.</p> <p>Here is the source, in case you're interested:</p> <pre><code>def convertToPacient = { withPerson(params.id) {person -&gt; def pacient = new Pacient() pacient.properties = person.properties pacient.id = null pacient.processNumber = params.processNumber def ap = new Appointment(params) pacient.addToAppointments(ap); Person.withTransaction {tx -&gt; if (pacient.validate() &amp;&amp; !pacient.hasErrors()) { //to avoid the "Found two representations of same collection" error //pacient.attachments = new HashSet(person.attachments); //pacient.memberships = new HashSet(person.memberships); def groups = person?.memberships?.collect {m -&gt; Group.get(m.group.id)} def attachs = [] person.attachments.each {a -&gt; def att = new Attachment() att.properties = a.properties attachs &lt;&lt; att } //need an in in order to add the person to a group person.delete(flush: true) pacient.save(flush: true) groups.each {g -&gt; pacient.addToGroup(g)}; attachs.each {a -&gt; pacient.addToAttachments(a)} //pacient.attachments.each {att -&gt; att.id = null; att.version = null; att.person = pacient}; if (!pacient.save()) { tx.setRollbackOnly() return } } } </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.
    1. 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