Grails——主键问题

Grails——主键问题,grails,Grails,我有一个名为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

我有一个名为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名称+工作区Id”的组合检索实体,该组合是OrgUnit实体的次关键字,而不是主键字段“Id”。因为名称是一个被更改的字段,所以生成的查询无法检索任何内容,提交以静默方式失败,我没有得到任何异常

下面是作为合并操作的一部分生成的DB查询

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为什么会忽略主键,并尝试基于次键检索实体

任何想法都值得赞赏

谢谢,
Kishore

Grails并没有试图检索这个。它是唯一的约束-‘每个工作区的名称’在保存前被选中


看起来save()最终还是失败了。它真的会默默地失败吗?那么mergedOu.hasErrors()呢?

谢谢您的回复。它确实是默默地失败了。mergedOu.hasErrors()返回false。我正在查看p6spy日志,提交似乎没有发出update语句。我不知道为什么。这可能是Grails merge()功能的问题。通常的范例是,如果您想要更新一个实体,那么在控制器中,您有一个作为参数传入的属性映射。基于params.id从数据库检索对象,然后将params中的属性复制到对象中并保存它。如果我试着在我的服务课上模仿这一点,它似乎工作得很好。我似乎找不到任何使用merge()的Grails示例。顺便说一句,我使用的是Grails1.3.3。如果其他任何人使用相同的Grails版本,并且合并工作正常,我想知道。多谢!那么
mergedOu.validate()和&mergedOu.hasErrors()
呢?实际上,Grails使用了另一种方法,比如
defou=OrgUnit.get(id);ou.properties=params
。也许可以在您的案例中使用
properties=something
解决方法?