[英]Hibernate same object with different values
I have one class with specific columns, say 我有一堂课有特定的栏目
Class A
{
private String A;
private String B;
private String C;
// Getter Setter of respectives
}
Now what happened I have same value of column A and column B only column C 's value change. 现在发生了什么事,我只有列C和C的值更改了,而列A和B的值相同。 So I do something like below 所以我做下面的事情
A a = new A();
a.setA(..);
a.setB(..);
for(i=0;i<length;i++){
a.setC(..);
getHibernateTemplate.saveOrUpdate(a);
// or something like this
// A a1 = new A();
// a1 = a;
// a1. setC(..);
// getHibernateTemplate.saveOrUpdate(a1);
}
My issue is it does not store length number of records, it only updates that single record. 我的问题是它不存储记录的长度,它仅更新该单个记录。
I know the reason that hibernate access it as persistent object and even if I change value and again save it will update existing record and it can be resolve by taking new object every time and setting it all values . 我知道休眠作为永久对象访问它的原因,即使我更改值并再次保存它也会更新现有记录,并且可以通过每次都使用新对象并将其设置为所有值来解决。 But I don't want it, is there any way to tell hibernate to save that record instead of updating? 但是我不想要它,有什么办法告诉hibernate保存该记录而不是进行更新吗?
You haven't described actual entity details. 您尚未描述实际的实体详细信息。 If you want to save entity with the same values, set the identifier property as null
. 如果要保存具有相同值的实体,请将identifier属性设置为null
。
From Documentation - 从文档-
saveOrUpdate() saveOrUpdate()
saveOrUpdateAll() saveOrUpdateAll()
[ If it works, can try this for your other query ] [ 如果有效,可以尝试其他查询 ]
Edit : It's mine oversight, I haven't checked your code carefully. 编辑:这是我的疏忽,我没有仔细检查您的代码。 You have defined object A
outside for loop, therefore the same object was being updated in each iteration. 您已经在循环外定义了对象A
,因此在每次迭代中都将更新同一对象。 Try the below code, might help. 尝试以下代码,可能会有所帮助。
for(i=0;i<length;i++){
A a = new A(); //-- Create new object for each iteration
a.setA(..);
a.setB(..);
a.setC(..);
getHibernateTemplate.saveOrUpdate(a);
}
是的,尝试使用save(a)
而不是saveOrUpdate(a)
getHibernateTemplate.save(a); //each time a new object saves.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.