各位好,好久沒有上來跟大家交流,最近遇了一個問題,一直找不到原因
我有一個delete atction (是從acegi plugin 挑出來的)
/**
* Person delete action. Before removing an existing person,
* he should be removed from those authorities which he is involved.
*/
def delete = {
def person = CmUser.get(
params.id)
if (person) {
def authPrincipal = authenticateService.principal()
//avoid self-delete if the logged-in user is an admin
if (!(authPrincipal instanceof String) && authPrincipal.username == person.username) {
flash.message = "You can not delete yourself, please login as another admin and try again"
}
else {
//first, delete this person from People_Authorities table.
try{
CmRole.findAll().each { it.removeFromPeople(person) }
person.delete(flush:true)
println "test!!!"
flash.message = "CmUser $params.id deleted."
redirect action: list
}
catch(org.springframework.dao.DataIntegrityViolationException e){
flash.message = "訪視員不能被刪除"
flash.args = [params.id]
redirect(action: "show", id: params.id)
return
}
}
}
else {
flash.message = "CmUser not found with id $
params.id"
redirect action: list
}
}
就是刪除一個User 但這個user 當了別的foreign key 所以不能刪除
所以我加了一個try catch這段execption 可是網頁卻一直導向到錯誤頁面
可是使用者應該不想看這麼多密密麻麻錯誤訊息
我有寫
flash.message = "訪視員不能被刪除"
flash.args = [params.id]
redirect(action: "show", id: params.id)但是始終導不過去,請問各位前輩能否指點迷津 謝謝
Grails Runtime Exception
Error Details
Error 500: Servlet:
default
URI: /childminder/cmUser/index
Exception
Message: Cannot delete or update a parent row: a foreign key constraint
fails (`childminder`.`cm_meical_check`, CONSTRAINT `FK508FE45B2F6A8D41` FOREIGN
KEY (`create_user_id`) REFERENCES `cm_user` (`id`))
Caused
by: Executing action [delete] of controller [CmUserController] caused
exception: org.springframework.dao.DataIntegrityViolationException: could not
delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?];
constraint [null]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not delete:
[CmUser#1]
Class: CmUserController
At
Line: [68]
Code Snippet:
67: redirect(action: "show", id:
params.id)
68:
return
69: }
Stack Trace
org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Executing action [delete] of controller [CmUserController] caused exception: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
at java.lang.Thread.run(Thread.java:619)
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
... 1 more
Caused by: org.springframework.dao.DataIntegrityViolationException: could not delete: [CmUser#1]; SQL [delete from cm_user where id=? and version=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
at CmUserController$_closure4.doCall(CmUserController:68)
at CmUserController$_closure4.doCall(CmUserController)
... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [CmUser#1]
at $Proxy10.flush(Unknown Source)
... 3 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`childminder`.`cm_meical_check`, CONSTRAINT `FK508FE45B2F6A8D41` FOREIGN KEY (`create_user_id`) REFERENCES `cm_user` (`id`))
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1016)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
--
您收到此邮件是因为您订阅了 Google 网上论坛的“Grails联盟-GrailsUnion”论坛。
要向此网上论坛发帖,请发送电子邮件至
grail...@googlegroups.com。
要取消订阅此网上论坛,请发送电子邮件至
grailsunion...@googlegroups.com。
若有更多问题,请通过
http://groups.google.com/group/grailsunion?hl=zh-CN 访问此网上论坛。