开发者

Grails -- Issue with secondary key

I have an entity named OrgUnit defined as follows

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)
    }
...
...

}

In my service class..

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();
    }

}

The OrgUnit that I am trying to update has the name and some other details changed. It seems like a pretty simple update but it does not work. The reason being, as part of the merge, Grails tries to retrieve an entity based on the combination of 'OrgUnit Name + WorkSpace Id' which is the secondary key rather than the primary key field 'Id' of the OrgUnit entity. And because the name is one fields that is changed, the query generated fails to retrieve anything, the commit fails silently and I get no exception at all.

Here is the DB Query being generated as part of the merge operation..

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 t开发者_高级运维his_.name=? and this_.work_space_id=?

I am not sure why Grails is ignoring the primary key and tries to retrieve the entity based on the secondary key.

Any ideas appreciated.

Thanks, Kishore


Grails is not trying to retrieve this one. It's the unique constraint - 'name for each Workspace' checked before save.

And looks like save() fails after all. Does it really fail silently? What about mergedOu.hasErrors()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜