问题ORA-00001:INSERT/UPDATE中违反了唯一约束

pri*_*dev 6 oracle hibernate transactions

我试图通过应用程序在表中插入一些值并得到问题ORA-00001:违反了唯一约束.我看到序列与表的最高id不同步,但即使修复了序列号,错误仍然存​​在.如何更多地调试此错误,oracle日志是否会产生更多错误?我怎么能看到oracle日志?谢谢Priyank

更新:我们正在使用审计日志插件,在User类的域类中,我们捕获save事件并将条目记录到审计日志中

所以在User类中我们做:

class User {

//some attributes, constraints, mappings

def onSave = {
 Graaudit aInstance = new Graaudit();
         aInstance.eventType= "GRA User Create"
         aInstance.eventDescription = "GRA User Created"
         aInstance.objectid = username
         aInstance.objecttype = 'GRAUSER'
         aInstance.user_id = RequestContextHolder.currentRequestAttributes().session.username

          aInstance.withTransaction{
              aInstance.save()
          }
    }

}
Run Code Online (Sandbox Code Playgroud)

当我们在onSave事件中没有上述代码时,用户就会成功创建.
我假设它与我们在aInstance上使用的hibernate事务有关,即死亡或当前事务因为该保存而死亡.
如果我们不使用该交易我们得到一个例外"org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" 不确定如何解决这个问题..谢谢

Jus*_*ave 11

错误消息将包括违反的约束的名称(表上可能有多个唯一约束).您可以使用该约束名称来标识声明唯一约束的列

SELECT column_name, position
  FROM all_cons_columns
 WHERE constraint_name = <<name of constraint from the error message>>
   AND owner           = <<owner of the table>>
   AND table_name      = <<name of the table>>
Run Code Online (Sandbox Code Playgroud)

一旦知道了哪些列受到影响,您就可以比较您尝试使用的数据INSERTUPDATE与表中已有的数据进行比较,以确定违反约束的原因.

  • 或者,您可以使用此查询:`SELECT*FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.CONSTRAINT_NAME LIKE'%SYS_C0010988%';`.只需在最后替换`CONSTRAINT_NAME`:`SYS_C0010988`,并在错误消息中添加一个. (5认同)

Bal*_*yan 7

由于违反唯一约束而发生此 ORA 错误。

ORA-00001: unique constraint (constraint_name) violated
Run Code Online (Sandbox Code Playgroud)

这是因为尝试执行在受唯一索引限制的字段中创建了重复值的INSERTorUPDATE语句。

您可以通过以下方式解决此问题

  • 更改约束以允许重复,或
  • 删除唯一约束,或者您可以更改 SQL 以避免重复插入