我正在开发Grails 2.3.7应用程序,我无法通过选择框更改域属性。每次我尝试更改属性并保存时,我都会得到一个HibernateException: identifier of an instance of Ethnicity was altered from X to Y
。我不想改变种族的身份,我只是想把种族从一个变成另一个种族。无法更改Grails中的域类属性值
有几件事情需要注意:我使用的是同一个控制器的动作来创建和更新的人
- 。
- 设置
personInstance.ethnicity
到null
权personInstance.properties = params
前将保存的工作,但我 不知道为什么,我不想为每个协会 ,我想改变这一点。 - 我意识到域模型似乎很奇怪。这是一个传统的数据库,我无法改变。
这里是我的领域类:
class ApplicationPerson implements Serializable {
Integer appId
Integer applicationSequenceNumber
String firstName
String lastName
Ethnicity ethnicity
static mapping = {
id composite: ['appId', 'applicationSequenceNumber'],
generator: 'assigned'
}
}
class Ethnicity {
String code
String description
static mapping = {
id name: 'code', generator: 'assigned'
}
}
这是我_form.gsp
更新Ethnicity
(我删除了所有正在保存就好了其他属性):
<div class="fieldcontain ${hasErrors(bean: personInstance,
field: 'ethnicity', 'error')} ">
<label for="ethnicity">Ethnicity</label>
<g:select id="ethnicity"
name="ethnicity.code"
from="${Ethnicity.list()}"
optionKey="code"
value="${personInstance?.ethnicity?.code}" />
</div>
最后一点,我的控制器动作表单POST:
def save() {
Application app = applicationService.getCurrentApplication()
// Find/Create and save Person
ApplicationPerson personInstance = app.person
if (!personInstance) {
personInstance =
new ApplicationPerson(appId: app.id,
applicationSequenceNumber: app.sequenceNumber)
}
personInstance.properties = params
if (!personInstance.validate()) {
respond personInstance.errors, view:'edit'
return
}
personInstance.save flush:true
redirect action: 'list'
}
组合键中的'aidm'是什么? – dmahapatro
一个错字。感谢您指出了这一点。 – grantmcconnaughey
你使用迁移脚本吗?以及如何备份后自动创建数据库...? – danielad