Java Hibernate:如何修复“从 X 更改为 Y 的实例的标识符”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4179166/
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
Hibernate: How to fix "identifier of an instance altered from X to Y"?
提问by gennad
org.hibernate.HibernateException: identifier of an instance
of org.cometd.hibernate.User altered from 12 to 3
in fact, my user
table is really must dynamically change its value, my Java app is multithreaded.
Any ideas how to fix it?
实际上,我的user
表确实必须动态更改其值,我的 Java 应用程序是多线程的。任何想法如何解决它?
采纳答案by Juha Syrj?l?
Are you changing the primary key value of a User object somewhere? You shouldn't do that. Check that your mapping for the primary key is correct.
您是否在某处更改 User 对象的主键值?你不应该那样做。检查您的主键映射是否正确。
What does your mapping XML file or mapping annotations look like?
您的映射 XML 文件或映射注释是什么样的?
回答by Danubian Sailor
You must detach your entity from session before modifying its ID fields
在修改其 ID 字段之前,您必须从会话中分离您的实体
回答by Ethereal
In my particular case, this was caused by a method in my service implementation that needed the spring @Transactional(readOnly = true)
annotation. Once I added that, the issue was resolved. Unusual though, it was just a select statement.
在我的特殊情况下,这是由我的服务实现中需要 spring@Transactional(readOnly = true)
注释的方法引起的。一旦我添加了它,问题就解决了。不寻常的是,它只是一个选择语句。
回答by Muhammad Hewedy
In my case, the PK Field in hbm.xml was of type "integer" but in bean code it was long
.
在我的例子中,hbm.xml 中的 PK 字段是“整数”类型,但在 bean 代码中它是long
.
回答by Andres
In my case, I solved it changing the @Id field type from long to Long.
就我而言,我解决了将 @Id 字段类型从 long 更改为 Long 的问题。
回答by nostromo
In my case, a template had a typo so instead of checking for equivalency (==) it was using an assignment equals (=).
在我的例子中,模板有一个错字,所以不是检查等效性 (==),而是使用赋值等于 (=)。
So I changed the template logic from:
所以我改变了模板逻辑:
if (user1.id = user2.id) ...
to
到
if (user1.id == user2.id) ...
and now everything is fine. So, check your views as well!
现在一切都很好。所以,也要检查你的观点!
回答by Anuruddha Lanka Liyanarachchi
In my case getters and setter names were different from Variable name.
在我的情况下,getter 和 setter 名称与变量名称不同。
private Long stockId;
public Long getStockID() {
return stockId;
}
public void setStockID(Long stockID) {
this.stockId = stockID;
}
where it should be
它应该在哪里
public Long getStockId() {
return stockId;
}
public void setStockId(Long stockID) {
this.stockId = stockID;
}
回答by Ken
Make sure you aren't trying to use the same User object more than once while changing the ID. In other words, if you were doing something in a batch type operation:
确保在更改 ID 时不要尝试多次使用同一个 User 对象。换句话说,如果您在批处理类型操作中执行某些操作:
User user = new User(); // Using the same one over and over, won't work
List<Customer> customers = fetchCustomersFromSomeService();
for(Customer customer : customers) {
// User user = new User(); <-- This would work, you get a new one each time
user.setId(customer.getId());
user.setName(customer.getName());
saveUserToDB(user);
}
回答by KgaboL
In my case it was because the property was long on object but int in the mapping xml, this exception should be clearer
在我的情况下,这是因为该属性在对象上很长,但在映射 xml 中是 int,这个异常应该更清楚
回答by bully
I was facing this issue, too.
我也面临这个问题。
The target table is a relation table, wiring two IDs from different tables. I have a UNIQUE constraint on the value combination, replacing the PK. When updating one of the values of a tuple, this error occured.
目标表是一个关系表,连接来自不同表的两个 ID。我对值组合有一个 UNIQUE 约束,取代了 PK。更新元组的其中一个值时,发生此错误。
This is how the table looks like (MySQL):
这是表的样子(MySQL):
CREATE TABLE my_relation_table (
mrt_left_id BIGINT NOT NULL,
mrt_right_id BIGINT NOT NULL,
UNIQUE KEY uix_my_relation_table (mrt_left_id, mrt_right_id),
FOREIGN KEY (mrt_left_id)
REFERENCES left_table(lef_id),
FOREIGN KEY (mrt_right_id)
REFERENCES right_table(rig_id)
);
The Entity class for the RelationWithUnique
entity looks basically like this:
实体的实体类RelationWithUnique
基本上是这样的:
@Entity
@IdClass(RelationWithUnique.class)
@Table(name = "my_relation_table")
public class RelationWithUnique implements Serializable {
...
@Id
@ManyToOne
@JoinColumn(name = "mrt_left_id", referencedColumnName = "left_table.lef_id")
private LeftTableEntity leftId;
@Id
@ManyToOne
@JoinColumn(name = "mrt_right_id", referencedColumnName = "right_table.rig_id")
private RightTableEntity rightId;
...
I fixed it by
我修好了
// usually, we need to detach the object as we are updating the PK
// (rightId being part of the UNIQUE constraint) => PK
// but this would produce a duplicate entry,
// therefore, we simply delete the old tuple and add the new one
final RelationWithUnique newRelation = new RelationWithUnique();
newRelation.setLeftId(oldRelation.getLeftId());
newRelation.setRightId(rightId); // here, the value is updated actually
entityManager.remove(oldRelation);
entityManager.persist(newRelation);
Thanks a lot for the hint of the PK, I just missed it.
非常感谢PK的提示,我刚刚错过了。