gpt4 book ai didi

java - @OrderColumn 生成更新主键的请求

转载 作者:IT老高 更新时间:2023-10-28 20:54:56 29 4
gpt4 key购买 nike

我使用列表。该列表由一个复合主键组成,该主键也用于对列表进行排序。问题是如果我删除列表中的一个元素(关键化合物),注解 @OrderColumn 生成更新主键的请求,成本上升异常类型:

[26-05-2011 10:34:18:835] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 1062, SQLState: 23000  
[26-05-2011 10:34:18:835] ERROR org.hibernate.util.JDBCExceptionReporter -Duplicate entry '10-10' for key 'PRIMARY'
[26-05-2011 10:34:18:835] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

这是映射的定义:

@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "chapter_item", joinColumns = { @JoinColumn(name = "chapter_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "item_id", nullable = false, updatable = false) })
@OrderColumn(name="iorder")
public List<Item> getItems() {
return items;
}

这是我遇到问题的更新查询:

Hibernate: 
update
chapter_item
set
item_id=?
where
chapter_id=?
and iorder=?

我想知道这是否是一个已知的错误,是否有人有解决方案?

最佳答案

关于@OrderColumn,以下文档位于http://docs.oracle.com/javaee/6/api/javax/persistence/OrderColumn.html

Specifies a column that is used to maintain the persistent order of a list. The persistence provider is responsible for maintaining the order upon retrieval and in the database. The persistence provider is responsible for updating the ordering upon flushing to the database to reflect any insertion, deletion, or reordering affecting the list.

所以我们看到持久化提供者,即hibernate负责更新名为iorder的列。 .也有人说:

The OrderColumn annotation is specified on a OneToMany or ManyToMany relationship or on an element collection. The OrderColumn annotation is specified on the side of the relationship that references the collection that is to be ordered. The order column is not visible as part of the state of the entity or embeddable class.

请注意下面的句子:

The order column is not visible as part of the state of the entity or embeddable class.

所以,我建议您考虑不选择 iorder 列吗?对于 @OrderColumn因为它是您的composite key 的一部分并且当您在列表中删除或插入元素时,hibernate 肯定会更新此值 (List<Item>)。

希望,这会有所帮助。

关于java - @OrderColumn 生成更新主键的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136048/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com