Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 违反唯一密钥约束_Sql_Hibernate_Grails - Fatal编程技术网

Sql 违反唯一密钥约束

Sql 违反唯一密钥约束,sql,hibernate,grails,Sql,Hibernate,Grails,我正在使用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

我正在使用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)  
          }

我建议改变你的限制

  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)  
  }

需要查看保存对象的代码从我的错误消息中很难分辨-是抱怨空值还是非唯一值?我做了您建议的更改-它一定违反了唯一性约束,因为我从未进入type==null条件块我不知道从何处获取“type”值,您应该检查它是否是字符串“null”