나는 유지해야 할 Grails 1.3.9 프로젝트를 상속 받았다.grails GORM : 수퍼 클래스의 ID에 액세스하는 방법
확장 된 약속 생성을 기록하기 위해 컨트롤러 중 하나를 확장해야하는 경우가 있습니다.
약속은 다음과 같이 정의된다 :
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
}
}
}
특별 예약 :
class SpecialAppointment extends Appointment implements Serializable {
static mapping = {
table 'special_appointment'
cache true
columns{
id column: 'pt_id'
comment column: 'pt_name'
// other columns
}
}
}
역사 로그 : 컨트롤러에서
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
}
}
}
이 같은 약속을 가지고 SpecialAppointment을 만들 수는 기본 클래스, AppointmentHistory의 새 인스턴스를 만들고 저장해야합니다. 약속과의 관계. 대신 약속의 ID의 역사 개체를 만들 때
def app = new SpecialAppointment()
// set fields here
app.save(flush:true)
// save history log
def history = new AppointmentHistory(appointment: app)
나는 SpecialAppointment의 인스턴스를 전달하지만, 그것의 ID를 사용하기 때문에 그것은 잘못된 것입니다.
불행히도 올바른 구문을 알아낼 수 없어서 파생 클래스의 저장된 인스턴스에서 수퍼 클래스의 멤버에 액세스했습니다.
알려 주시기 바랍니다.
Extension보다는 Composition이어야합니다. SpecialAppointment는 Appointment의 하위 클래스가 아니어야하며, Appointment에 대한 참조 (nullable이 아닌)를 보유하고있는 별도의 클래스 여야합니다. –
네, 그런 식으로 구현하는 것이 더 좋을 수도 있지만, 이것은 더 큰 응용 프로그램의 일부이며 이러한 관계를 변경하지는 않을 것입니다. 수퍼 클래스 멤버에게 명시 적으로 액세스하는 올바른 구문은 무엇입니까? – matejk