2015-10-16 47 views
0

我有两个域:如何获取类的实例而不是其子类?

事件:

class Incident { 
    String title 
    String description 
} 

static mapping = { 
    tablePerHierarchy(false) 
} 

而且IncidentWithTimers延伸事件:

class IncidentWithTimers extends Incident { 
    int currentResponseTime 
    Date responseTimeEndDate 
} 

IncidentWithTimers是不是在数据库中实际的表,这是一个数据库视图。由于该领域是一个看法,我通过在beforeInsert/Update抛出异常禁用所有修改

def index() { 
    Incident curIncident = Incident.get(params.incident) 
    println(curIncident.getClass())// "class IncidentWithTimers" 
} 

现在,当我尝试从一个控制器获得一个事件实例,它在某种程度上返回IncidentWithTimers实例/ Delete,在IncidentWithTimers类中。

当我尝试修改并保存curIncident时,它尝试访问beforeUpdate(),并引发错误,而我从来不想首先更改IncidentWithTimers

我该怎么办?

+0

'params.incident'中的事件ID是指'Incident'还是IncidentWithTimers'的实例? –

+0

都不是。 params.incident从查询字符串中获取事件的值。 (所以url.com/?incident=1会返回1)。 – Ivar

+0

我的意思是,按照你的例子,ID 1是事件或IncidentWithTimers的事件?另外,“IncidentWithTimers”被映射为来自“事件”映射到的同一个表的视图返回的ID是什么?是的,我暗示两者应共享相同的ID“名称空间”。 –

回答

1

问题是该视图包含每个Incident,而不是子类的子集。如您所说,每个IncidentWithTimers是一个Incident

解决方案是使用查询而不是子类,或者使得一个独立的重复类而不是子类。

我认为前者是最好的选择,因为意图是明确的。