简体   繁体   English

休眠具有不同值的同一对象

[英]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. 现在发生了什么事,我只有列CC的值更改了,而列AB的值相同。 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()

  • if the object is already persistent in this session, do nothing 如果对象在此会话中已经存在,则不执行任何操作
  • if another object associated with the session has the same identifier, throw an exception 如果与该会话关联的另一个对象具有相同的标识符,则引发异常
  • if the object has no identifier property, save() it 如果对象没有标识符属性,请保存()
  • if the object's identifier has the value assigned to a newly instantiated object, save() it 如果对象的标识符具有分配给新实例化的对象的值,则将其保存()
  • if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it 如果对象由或进行版本控制,并且版本属性值与分配给新实例化的对象的值相同,则将其保存()
  • otherwise update() the object 否则update()对象

saveOrUpdateAll() saveOrUpdateAll()

  • Save or update all given persistent instances, according to its id (matching the configured "unsaved-value"?). 根据其ID(匹配已配置的“未保存值”?)保存或更新所有给定的持久性实例。 Associates the instances with the current Hibernate Session. 将实例与当前的休眠会话相关联。

[ 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.

相关问题 提取相同的对象休眠到不同的对象 - Fetching the same object Hibernate to different objects Hibernate NonUniqueObjectException:具有相同标识符的不同对象 - Hibernate NonUniqueObjectException: A different object with the same identifier 休眠-如何加载同一对象的不同视图 - Hibernate - how to load different view of same object Hibernate:在不同的会话中更新相同的对象 - Hibernate: update same object in different session 休眠-映射同一列的两个字段的不同值 - Hibernate - different values of two fields mapped the same column JPA Hibernate:具有相同标识符值的不同 object 已经与 Z21D6F40CFB5125082E45ZZA0E 关联 - JPA Hibernate : A different object with the same identifier value was already associated with the session Spring + Hibernate:具有相同标识符值的不同对象已经与会话相关联 - Spring + Hibernate : a different object with the same identifier value was already associated with the session 防止同一对象与hibernate中的两个不同会话相关联 - Prevent the same object from being associated to two different sessions in hibernate 使用带有Hibernate的Spring数据jpa的具有相同标识符的不同对象 - A different object with the same identifier using Spring data jpa with Hibernate Hibernate 错误:具有相同标识符值的不同 object 已与 session 相关联 - Hibernate Error: a different object with the same identifier value was already associated with the session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM