我被扔进了一个现有的grails项目,我遇到的一个问题是,当保存一些批处理时,我得到错误: Cannot set readonly property: programId
这是导致错误的保存代码段
// Create a batch
def batch = new Batch()
batch.name = session.batch_name
batch.startDate = new Date()
batch.endDate = new Date()
batch.programId = 120
if(batch.save()) {
...
}
Run Code Online (Sandbox Code Playgroud)
这是我的批处理域类
class Batch extends AbstractDomainObject{
String name
Date startDate
Date endDate
String comments
StatusType currentStatus
static belongsTo = [program:Program]
static constraints = {
name(blank:false,maxSize:100)
startDate()
endDate()
comments (nullable:true, maxSize:DEFAULT_SIZE_OF_COMMENTS)
currentStatus(nullable:true)
}
static transients= ["currentStatus"]
static mapping = {
id column:'batch_id', generator:'sequence', params:[sequence:'sq_batch']
currentStatus column:'status_type_id'
program column:'program_id'
statuses sort:'statusDate'
startDate type:'date'
endDate type:'date'
}
public String toString(){
return name
}
public Date createdDate(){
Date date=null
if(this?.statuses?.size()>0){
this?.statuses.each{
if(it.status.value==SystemConstants.STATUS_PENDING){
date = it.statusDate
}
}
}
return date
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不让我设置programId?
Bur*_*ith 12
programId是一个动态属性,它为您提供program实例的id 而不从数据库加载它,但它是一个没有setter的getter.如果要在不产生加载整个实例的成本的情况下设置引用,请使用以下代码:
batch.program = Program.load(120)
Run Code Online (Sandbox Code Playgroud)
load使用仅存储了id的代理,除非您调用方法或访问除以外的属性,否则不会访问数据库id.所以它可以在这里工作,因为Hibernate只需要它id最终运行的SQL插件.
| 归档时间: |
|
| 查看次数: |
3468 次 |
| 最近记录: |