问题描述
我有2个域名:事件:
class事件{
字符串标题
字符串描述
}
静态映射= {
tablePerHierarchy(false)
}
以及事件延伸的IncidentWithTimers:
class IncidentWithTimers extends Incident {
int currentResponseTime
Date responseTimeEndDate
}
IncidentWithTimers不是数据库中的实际表,它是数据库视图。
现在,当我尝试从控制器获取事件实例时,它以某种方式返回IncidentWithTimers实例:
def index(){
事件curIncident = Incident.get(params.incident)
println(curIncident.getClass())//class IncidentWithTimers
}
由于该域是一个视图,因此我通过在<$ c中引发异常来禁用所有修改在IncidentWithTimers类中,$ c> beforeInsert / 更新
/ 删除
。
当我尝试修改并保存curIncident时,它会尝试访问 beforeUpdate()
,并引发错误,同时我从来不希望首先更改 IncidentWithTimers
。
我能做些什么?
问题是该视图包含每个事件
,而不是它们的子集就像子类一样。正如你所说,每一个 IncidentWithTimers
都是事件
。
解决方案是使用查询而不是子类,或者使 IncidentWithTimers
为独立的重复类而不是一个子类。
我认为前者是最好的选择,因为其意图很明确。
I have 2 domains:
Incident:
class Incident {
String title
String description
}
static mapping = {
tablePerHierarchy(false)
}
And IncidentWithTimers which extends Incident:
class IncidentWithTimers extends Incident {
int currentResponseTime
Date responseTimeEndDate
}
IncidentWithTimers is not an actual table in the database, it's a database view.
Now when I try to get an Incident instance from a controller, it somehow returns an IncidentWithTimers instance:
def index() {
Incident curIncident = Incident.get(params.incident)
println(curIncident.getClass())// "class IncidentWithTimers"
}
Since this domain is a view, I disabled all modifications by throwing exceptions in the beforeInsert
/Update
/Delete
, in the IncidentWithTimers class.
When I try to modify and save the curIncident, it tries to access the beforeUpdate()
, and throws the error, while I never wanted to change the IncidentWithTimers
in the first place.
What can I do about this?
The problem is that the view includes every Incident
, not a subset of them as would happen with a subclass. As you said, every IncidentWithTimers
is an Incident
.
The solution is to either use a query instead of a subclass, or make IncidentWithTimers
an independent duplicate class rather than a subclass.
I think the former is the best option because the intent is clear.
这篇关于我如何获得一个类的实例而不是它的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!