【发布时间】:2011-03-11 07:53:38
【问题描述】:
我有一个名为 OrgUnit 的实体,定义如下
class OrgUnit {
String name
String description
.....
.....
static belongsTo = [workSpace:WorkSpace]
static constraints = {
//enforce uniqueness of the OrgUnit Name within a WS.
name(nullable: false, blank: false, unique: 'workSpace')
address(nullable:true)
}
...
...
}
在我的服务班里..
def updateOrgUnit(OrgUnit orgUnit) {
//The DB query generated by GORM here is wrong...
OrgUnit mergedOu = OrgUnit.merge(orgUnit);
try
{
if (!mergedOu.save(flush:true)) {
mergedOu.errors.allErrors.each{error ->
println "An error occured while saving 'orgUnit': ${error}"
errorMsg = "Exception occurred while executing updateOrgUnit()";
}
}
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
我尝试更新的 OrgUnit 的名称和其他一些详细信息已更改。 这似乎是一个非常简单的更新,但它不起作用。原因是,作为合并的一部分,Grails 尝试根据“OrgUnit Name + WorkSpace Id”的组合检索实体,该组合是辅助键,而不是 OrgUnit 实体的主键字段“Id”。而且由于名称是一个已更改的字段,因此生成的查询无法检索任何内容,提交无声无息地失败,我一点也不例外。
这是作为合并操作的一部分生成的数据库查询..
select this_.id as id18_0_, this_.version as version18_0_, this_.address_id as address3_18_0_, this_.archive as archive18_0_, this_.name as name18_0_, this_.org_hierarchy_version as org14_18_0_,......this_.type as type18_0_, this_.work_space_id as work19_18_0_, this_.zoom as zoom18_0_ from org_unit this_ where this_.name=? and this_.work_space_id=?
我不确定为什么 Grails 会忽略主键并尝试根据辅助键检索实体。
任何想法表示赞赏。
谢谢, 基肖尔
【问题讨论】:
标签: grails