org.hibernate.exception.ConstraintViolationException:无法执行 JDBC 批量更新
在我正在处理的模块中,我收到此错误,据称该错误是由 org.hibernate.exception.ConstraintViolationException: 无法执行 JDBC 批量更新引起的 和 java.sql.BatchUpdateException
(完整的堆栈跟踪位于此处:单击此处< /a>)。
从我从其他帖子中读到的内容来看,这肯定是由于违反主键造成的。然而我什至无法复制这个问题,这样我至少可以追踪真正的错误并解决问题。每次我插入与数据库中已有的条目相同的条目时,它都会简单地相互合并并且不会出现错误。然而,我在部署中遇到了很多这样的错误,所以我不太确定部署服务器中发生了什么(我只是一名学生开发人员,所以我在这方面仍然有点“菜鸟”)。
即使有人能指出我的方向,我也将不胜感激。谢谢。 (如果需要添加某些内容,请告诉我)
这是模块的 hibernate 映射片段(希望这会有所帮助):
<hibernate-mapping package="edu.umd.cattlab.schema.cattXML.extensions.VaTraffic"
default-lazy="false" default-cascade="all, delete-orphan" schema="vatraffic">
<typedef class="edu.umd.cattlab.schema.hibernate.util.XMLGregorianCalendarType" name="XMLCal"/>
<class name="VaTrafficAssociatedEvent" table="associated_event">
<id name="associatedEventId" column="associated_event_id">
<generator class="sequence">
<param name="sequence">ritis.associated_event_id_seq</param>
</generator>
</id>
<property name="secondaryEventId" column="secondary_event_id" not-null="true" />
<property name="unassociatedTimestamp" type="XMLCal" column="unassociated" />
<property name="autoRelated" column="auto_related" not-null="true" />
<many-to-one name="relationshipType" column="relationship_type" not-null="true" cascade="none" />
</class>
这是利用上述映射的 java 代码部分: 点击此处
In my module that I'm working on, I got this error, which is said caused by org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
and java.sql.BatchUpdateException
(the full stack trace is in here : click here).
From what I've read from other posts, this is surely caused by violation of primary key. However I couldn't even replicate the problem so that I can at least trace the real bug and solve the problem. Every time I inserted an identical entry with the one which already in the database, it will simply merge with each other and no error. However, I was getting many of this error in the deployment, so I'm not quite sure what's going on in the deployment server (I'm just a student developer, so I'm still kinda 'noob' in this).
I would appreciate it even if anyone can point me to the direction. Thanks. (Inform me if there's something need to be added)
Here is the snippet of hibernate mapping for the module (hope this would help) :
<hibernate-mapping package="edu.umd.cattlab.schema.cattXML.extensions.VaTraffic"
default-lazy="false" default-cascade="all, delete-orphan" schema="vatraffic">
<typedef class="edu.umd.cattlab.schema.hibernate.util.XMLGregorianCalendarType" name="XMLCal"/>
<class name="VaTrafficAssociatedEvent" table="associated_event">
<id name="associatedEventId" column="associated_event_id">
<generator class="sequence">
<param name="sequence">ritis.associated_event_id_seq</param>
</generator>
</id>
<property name="secondaryEventId" column="secondary_event_id" not-null="true" />
<property name="unassociatedTimestamp" type="XMLCal" column="unassociated" />
<property name="autoRelated" column="auto_related" not-null="true" />
<many-to-one name="relationshipType" column="relationship_type" not-null="true" cascade="none" />
</class>
This is the part of java code that utilizes the above mapping: click here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以有更多的约束,而不仅仅是主键约束。可能是您违反了外键约束吗?或者可能是多列唯一约束。您能否包含您正在更新的表的 DDL?
You can have more constraints than just a primary key constraint. Could it be you have a foreign key constraint that you are violating? Or maybe a multiple column unique constraint. Could you please include the DDL for the table you are updating?
查看日志发现违反了pk约束。具体来说,错误:重复键违反了唯一约束“linked_event_pk”
尝试确定为什么会发生这种情况可能需要深入研究,但对于初学者来说,如何为该字段生成值?在您的 ddl 中,它显示为“nextval”字段,但您的日志似乎表明存在明确的值。这个值从哪里来?为什么你不让 postgre 自己设置这个值?
In reviewing the logs, it is a violation of the pk constraint. Specifically ERROR: duplicate key violates unique constraint "associated_event_pk"
Trying to determine why this is happening may be a deep dive, but for starters how are you generating values for this field? In your ddl it shows as a "nextval" field but your log appears to indicate there is an explicit value. Where is this value coming from? Why are you not letting postgre set the value itself?