0
我试图挽救它被映射喜欢 -Grails的错误“违反了外键约束”不能试图在同一时间
class Order {
String prefix
Long orderNumber
Date date
Incident incident
//..... few more variables
}
class Incident {
Double amountToCharge
String description
Boolean isCharged = false
Order order
//..... few more variables
}
现在域类对象保存多个对象时,我试图添加一个对象将它们保存我的服务类中我做了以下事项
def save(Order orderInstance, params){
orderInstance.withTransaction { status ->
if (orderInstance.save()) {
if(params.isIncident){
def incidentStatus = saveIncident(params, orderInstance)
if(incidentStatus.result == true){
println "-----------------Incident Saved"
}else{
status.setRollbackOnly()
result["result"] = false
result["message"] = "Could not save. Please try again."
return result
}
}
result["result"] = true
return result
} else {
for (fieldErrors in orderInstance.errors) {
for (error in fieldErrors.allErrors) {
result["message"] = messageSource.getMessage(error, locale)
}
}
return result
}
}
}
def saveIncident(params, Order orderInstance){
def result = [:]
def paramsMap = [:]
paramsMap.description = params.incident.description
paramsMap.amountToCharge = params.incident.amountToCharge
paramsMap.isCharged = params.incident.isCharged ? false : true
paramsMap.order = mROrderInstance
def incidentInstance = new Incident(paramsMap)
println "-----Done till here. Next line throws error----"
if (incidentInstance.save(flush:true)){
orderInstance.incident = incidentInstance
result["result"] = true
}else{
result["result"] = false
for (fieldErrors in incidentInstance.errors) {
for (error in fieldErrors.allErrors) {
println messageSource.getMessage(error, locale)
result["message"] = messageSource.getMessage(error, locale)
}
}
}
return result
}
它给了我下面的错误 -
ERROR: insert or update on table "order" violates foreign key constraint "fk_a0y0jvv0o9nhkafuj7g7iknc6"
Detail: Key (incident_id)=(2) is not present in table "incident".. Stacktrace follows:
Message: ERROR: insert or update on table "order" violates foreign key constraint "fk_a0y0jvv0o9nhkafuj7g7iknc6"
Detail: Key (incident_id)=(2) is not present in table "incident".
Line | Method
->> 2102 | receiveErrorResponse in org.postgresql.core.v3.QueryExecutorImpl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1835 | processResults in ''
| 257 | execute . . . . . . in ''
| 500 | execute in org.postgresql.jdbc2.AbstractJdbc2Statement
| 388 | executeWithFlags . . in ''
| 334 | executeUpdate in ''
| 124 | $tt__saveIncident . in com.xyz.fleet.db.OrderService$$EPZ0qsAS
| 46 | doCall in com.xyz.fleet.db.OrderService$_$tt__save_closure6$$EPZ0qsAS
| 815 | withTransaction . . in org.grails.datastore.gorm.GormStaticApi
| 715 | withTransaction in ''
| 41 | $tt__save . . . . . in com.xyz.fleet.db.MROrderService$$EPZ0qsAS
| 32 | $tt__save in com.xyz.fleet.db.MROrderController
| 198 | doFilter . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 53 | doFilter . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
| 49 | doFilter in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter
| 82 | doFilter . . . . . . in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
| 46 | doFilterInternal in org.grails.jaxrs.web.JaxrsFilter
| 1142 | runWorker . . . . . in java.util.concurrent.ThreadPoolExecutor
| 617 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run . . . . . . . . in java.lang.Thread
当我刷新会话时出现错误。即使我不那么每当它自动刷新然后它会抛出同样的错误。
注意:如果我没有手动刷新会话,则会保存Incident
,该方法返回保存Order
。它也可以保存。 Incident
对象附加到订购对象,我可以使用println
查询但最后它会自动清除会话,然后抛出上面显示的错误。
所以最后谁能告诉我这里有什么问题?
嗨,感谢您的回复。其实它不是空的情况,而是我创建'事件'对象和'Order'对象并附加它们。但是当hibernate会话刷新时,PostgreSQL会抛出(incident_id)=(2)在表“incident”中不存在的错误。肯定我会尝试添加一个映射到他们。 – Chetan
那么,如果你不使用'belongsTo',他们确保你在保存'Order'之前保存'Incident'。您可以*提前*附加*,但保存顺序非常重要。 –