主键已满的休眠saveOrUpdate [英] Hibernate saveOrUpdate with primary key filled

查看:151
本文介绍了主键已满的休眠saveOrUpdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表有两个字段
id - 唯一,NotNull和Seq
名称 - Varchar



在空表上,我发送记录为
id = 1(手动填写id值)
name ='sample'



我使用saveOrUpdate的意图是当数据库中没有Id:1存在的记录时,我需要用Id:1插入新行,如果不更新现有记录。

  DAO.getInstance()。saveOrUpdate(object,session); 

使用 saveOrUpdate 以下例外:

  ERROR  -  tractFlushingEventListener  - 无法使数据库状态与会话
同步org.hibernate.StaleObjectStateException:已更新行或由另一个事务删除(或未保存值的映射是不正确的):[com.floreantpos.model.MenuCategory#2]在org.hibernate.persister.entity.AbstractEntityPersister.check
(AbstractEntityPersister.java:1769)
。在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2412)
在org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312)
。在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612)
处org.hibernate.engine org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96)
。 ActionQueue.execute(ActionQueue.java:2 79)

但是,如果我使用save方法插入新行,这同样适用。



任何帮助都将非常可观。

解决方案

saveOrUpdate如果没有id,则在内存中存在id或插入新行。

saveOrUpdate()执行以下操作:



如果对象已经在这个会话中持久化,则不做任何事情



如果与会话相关的另一个对象具有相同的标识符,则抛出异常



如果对象没有标识符属性,则save()它如果对象的标识符具有赋值给新实例化对象的值,则保存它

()它



如果对象由or定义版本,并且版本属性值是分配给新实例化对象的相同值,则save()它



否则update()该对象


My table is having 2 fields id - unique, NotNull and Seq name - Varchar

On empty table I'm sending the record as id = 1 (Filling the id value manually) name = 'sample'

My intention of using saveOrUpdate is when there is no record with Id: 1 present in DB then I need to insert new row with Id: 1, if not update the existing record.

DAO.getInstance().saveOrUpdate(object, session);

On using saveOrUpdate I'm getting the following exception

ERROR - tractFlushingEventListener - Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.floreantpos.model.MenuCategory#2]
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1769)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2412)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)

But the same works if I use save method just to inset new row.

Any help will be really appreciable.

解决方案

saveOrUpdate() will do update if there is id present in the memory or insert new row if there is no id.

from documentation

saveOrUpdate() does the following:

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

这篇关于主键已满的休眠saveOrUpdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆