2010-10-26 276 views
0

的我使用的Grails,並試圖在我的控制器創建一個新的EducationType當我得到以下違反唯一鍵約束

2010-10-26 17:14:49,405 [http-8080-1] ERROR util.JDBCExceptionReporter - Violation of UNIQUE KEY constraint 'UQ__educat 
ion_type__0519C6AF'. Cannot insert duplicate key in object 'dbo.education_type'. 
2010-10-26 17:14:49,409 [http-8080-1] ERROR errors.GrailsExceptionResolver - could not insert: [EducationType]; SQL [in 
sert into education_type (version, name) values (?, ?)]; constraint [null]; nested exception is org.hibernate.exception. 
ConstraintViolationException: could not insert: [EducationType] 
org.springframework.dao.DataIntegrityViolationException: could not insert: [EducationType]; SQL [insert into education_t 
ype (version, name) values (?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationEx 
ception: could not insert: [EducationType] 
     at org.jsecurity.web.servlet.JSecurityFilter.doFilterInternal(JSecurityFilter.java:382) 
     at org.jsecurity.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:180) 
     at java.lang.Thread.run(Thread.java:619) 
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [EducationType] 
     ... 3 more 
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of UNIQUE KEY constraint 'UQ__education_type__0519 
C6AF'. Cannot insert duplicate key in object 'dbo.education_type'. 
     at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source) 
     at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source) 
     at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102) 
     ... 3 more 

這怎麼可能我收到這樣的消息,如果我有以下設置在我的控制器,以使這些對象:

class EducationType //the class 
{ 
    static constraints = 
    { 
     name(blank:false, unique:true) 
    } 
} 

//the block of code to do checking to not create duplicates 
      if(EducationType.findByName(type.toString())){ 
      edType = EducationType.findByName(type.toString()) 
      }else { 
       edType = new EducationType(name:type) 
      } 
+0

需要看到的是保存對象 – 2010-10-26 22:54:46

回答

1

我建議改變你的約束

static constraints = 
    { 
     name(blank:false, nullable:false, unique:true) 
    } 

我還要更改代碼以這種

if (type == null) { 
    // error handle, i think null will set the value 
    // of the type to the string "null" and that will get 
    // saved 
    return 
    } 


    if(EducationType.findByName(type?.toString())) 
     edType = EducationType.findByName(type.toString()) 
    } else { 
     edType = new EducationType(name:type) 
    } 
+0

它很難從我的錯誤信息告訴代碼 - 它是在抱怨空值,還是非唯一值? – Derek 2010-10-27 00:49:14

+0

你改變了你的代碼嗎? – 2010-10-27 00:52:45

+0

我做了你所建議的改變 - 它必須以某種方式違反唯一約束,因爲我從來沒有把它變成類型== null條件塊 – Derek 2010-10-27 18:20:14

相關問題