oracle 违反约束异常 ORA-00001

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10969693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:14:59  来源:igfitidea点击:

Constraint Violation Exception ORA-00001

oraclehibernateindexingora-00001

提问by Poorna

I am using Oracle database. We are seeing frequent failures in calls to our service. When I looked at the logs I am seeing following exceptions on a table

我正在使用 Oracle 数据库。我们经常看到调用我们的服务失败。当我查看日志时,我在表上看到以下异常

java.sql.BatchUpdateException: ORA-00001: unique constraint (DBSCHEMA.IDX_CO_DETAILS) violated.

java.sql.BatchUpdateException: ORA-00001: 违反了唯一约束 (DBSCHEMA.IDX_CO_DETAILS)。

I have checked the Index on the table for index name DBSCHEMA.IDX_CO_DETAILS .

我已经检查了表上的索引以获取索引名称 DBSCHEMA.IDX_CO_DETAILS 。

It did not include any column's( INCLUDE_COLUMN is null) . How can I know what is this constraint for ? Is it primary key constraint?

它不包括任何列的( INCLUDE_COLUMN 为空)。我怎么知道这个约束是什么?是主键约束吗?

We are using hibernate for ORM. Below is the back trace in hibernate context

我们正在为 ORM 使用休眠。以下是休眠上下文中的回溯

Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)

回答by Ben

A unique constraintenforces, well, uniqueness. It will allow nulls, unlike a primary key constraint.

一个独特的约束强制实施,那么,唯一性。与主键约束不同,它将允许空值。

Your error means that you are inserting duplicate data when the database has been configured to explicitly prohibit that.

您的错误意味着您在将数据库配置为明确禁止时插入了重复数据。

You can find out what constraints are on a table by running the following query on all_constraints. The link decodes the column CONSTRAINT_TYPE, for instance Pis a primary key and Ua unique key.

您可以通过在all_constraints上运行以下查询来找出表上的约束。该链接对列CONSTRAINT_TYPE进行解码,例如P是主键和U唯一键。

select *
  from all_constraints uc
 where uc.table_name = 'MY_TABLE'
   and owner = 'DBSCHEMA'

To find out what columns are in a constraint use all_cons_columnsinstead, or combining the two into one query:

要找出约束中的列,请all_cons_columns改用,或将两者合并为一个查询:

select uc.*, ucc.column_name, ucc.position
  from all_constraints uc
  join all_cons_columns ucc
    on uc.owner = ucc.owner
   and uc.table_name = ucc.table_name
   and uc.constraint_name = ucc.constraint_name
 where uc.table_name = 'MY_TABLE'
   and uc.owner = 'DBSCHEMA'

To either query you can add the additional condition and constraint_name = 'IDX_CO_DETAILS'to find out details of the specific constraint that seems to be causing your problem.

对于任一查询,您都可以添加附加条件and constraint_name = 'IDX_CO_DETAILS'以找出似乎导致问题的特定约束的详细信息。



Your comment is a little surprising for a couple of reasons. Even a system created constraint, for instance one that was defined in-line when the table was created without a name being specified should show up. Also, the constraint name IDX...implies that it's an index.

由于几个原因,您的评论有点令人惊讶。即使是系统创建的约束,例如在没有指定名称的情况下创建表时内嵌定义的约束也应该显示出来。此外,约束名称IDX...暗示它是一个索引。

IF you run the following query it should tell you if the object exists in the database:

如果您运行以下查询,它应该告诉您该对象是否存在于数据库中:

select *
  from all_objects
 where object_name = 'IDX_CO_DETAILS'

I would expect that the OBJECT_TYPEreturned by this query is 'INDEX'.

我希望OBJECT_TYPE这个查询返回的是'INDEX'.

Following on from that the following query will return every index with that name, the type of index, the table it is associated with and the owner of that table.

接下来的查询将返回具有该名称的每个索引、索引类型、与之关联的表以及该表的所有者。

select *
  from all_indexes
 where index_name = 'IDX_CO_DETAILS'

Judging by your error I would further expect that the column UNIQUNESSreturned by this query is 'UNIQUE'.

根据您的错误判断,我还希望UNIQUNESS此查询返回的列是'UNIQUE'.

This should help you track down the object.

这应该可以帮助您追踪对象。

You can also use the system package dbms_metadatato track down the DDL of the object; be careful it returns a clob.

也可以使用系统包dbms_metadata来追踪对象的DDL;小心它返回一个clob。

select dbms_metadata.get_ddl('INDEX','IDX_CO_DETAILS', schema => 'DBSCHEMA') 
  from dual

the parameter schemais optional.

该参数schema是可选的。