Note that there are some explanatory texts on larger screens.

plurals
  1. POgrails GORM: how to access id of super class
    text
    copied!<p>I inherited a Grails 1.3.9 project that I need to maintain.</p> <p>There is a situation when one of the controllers needs to be extended to log creations of extended appointments.</p> <p>The appointments are defined like this:</p> <pre><code>class Appointment implements Serializable{ static mapping = { table 'appointment' version false tablePerHierarchy false id generator:'sequence', params:[sequence:'seq_te_id'] cache true columns{ id column:'te_id' start column: 'te_start' // Other columns follow } } } </code></pre> <p>Special appointment:</p> <pre><code>class SpecialAppointment extends Appointment implements Serializable { static mapping = { table 'special_appointment' cache true columns{ id column: 'pt_id' comment column: 'pt_name' // other columns } } } </code></pre> <p>History log:</p> <pre><code>class AppointmentHistory { static mapping = { version false table 'appointment_history' id generator: 'sequence', params:[sequence:'seq_th_id'] cache true columns { id column: 'th_id' termin column: 'pt_id' // other columns } } } </code></pre> <p>In the controller to create SpecialAppointment, which has Appointment as its base class, I need to create and save new instance of AppointmentHistory, which has a relation to Appointment.</p> <pre><code>def app = new SpecialAppointment() // set fields here app.save(flush:true) // save history log def history = new AppointmentHistory(appointment: app) </code></pre> <p>I passed instance of SpecialAppointment when creating history object, but it is wrong, because it uses its ID, instead of ID of Appointment.</p> <p>Unfortunately I was not able to figure out <strong>proper syntax</strong> to access members of super class from just saved instance of derived class.</p> <p>Please advise.</p>
 

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