`
yinlongfei
  • 浏览: 155249 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

identifier of an instance of Entity is altered from 41 to 43

阅读更多
hibernate提交事务时报如下错误:
Caused by: org.hibernate.HibernateException:
identifier of an instance of user is alterde from from 41 to 43。

导致该错误的代码如下:
private User user = new Usesr();
public void save(){
  Session session=HibernateSessionFactory.getSession();
  Transaction tx = null;
  try {
    tx = session.beginTransaction();
    session.saveOrUpdate(user);
    tx.commit();
  } catch (HibernateException e) {
tx.rollback();
log.error("Error: " + e.getMessage());
  }
}

导致该错误的原因:
在使用seam框架中将一个action类的上下文范围定义page范围,访问一个注册页面,
添加注册信息,调用该action类保存后,如果页面还是返回到注册页面(即返回到原页面),
当再次填写注册信息保存时,由于该action类的上下文范围是page范围,上一次保存的user对象还存在,
导致再次保存user对象提交事务插入时hibernate认为第二次保存的对象与之前的对象是同一个对象,之前的主键被更新,因此报错。

解决办法:
在每次提交事务后再立即新建一个对象。

正确的代码如下:
private User user = new Usesr();
public void save(){
  Session session=HibernateSessionFactory.getSession();
  Transaction tx = null;
  try {
    tx = session.beginTransaction();
    session.saveOrUpdate(user);
    tx.commit();
    user = new User();
  } catch (HibernateException e) {
tx.rollback();
log.error("Error: " + e.getMessage());
  }
}

导致该错误的其它原因可能还有:
hibernate hbm里面id的type和java文件里面的不一致!
1
0
分享到:
评论

相关推荐

    Devart LinqConnect 3.1

    The "Identifier Case Sensitive" check box, which enables case-sensitive processing of database object identifier names, is added to MySQL Server Options The documentation generation for Enum members ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: Add TFlexPanel.InvalidateControl virtual method which calls from TFlexControl.Invalidate and can be overriden (it is possible now to catch all object invalidation calls). - FIX: The TFlexPanel....

    park an autonomous vehicle

    This research work is carried out within the framework of the PRAXITELE project which aims to develop a future-oriented urban transportation system based on a fleet of electric computer-driven ...

    计算机网络第六版答案

    Wifi (802.11) In a wireless LAN, wireless users transmit/receive packets to/from an base station (i.e., wireless access point) within a radius of few tens of meters. The base station is typically ...

    微软内部资料-SQL性能优化5

    Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...

    VclZip pro v3.10.1

    Modified so that when cursor is changed to hourglass by VCLZip, previous cursor is saved correctly instead of just changing it back to default cursor. Now saves Central Directory Extra Fields ...

    NewSID(光学习一下代码就可以了,没看清楚介绍别运行)

    All resources, including files and Registry keys, that one user has access to, the other will as well.Another instance where duplicate SIDs can cause problems is where there is removable media ...

    servlet2.4doc

    A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource. FilterConfig - interface javax.servlet....

    微软内部资料-SQL性能优化3

    An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes. Prior to SQL Server 7.0, REPEATABLE READ and ...

    Keil MDK 编译 ucOS-II 出现的警告和错误解决

    错误:L6238E:os_cpu_a.o(subr) contains invalid call from '~PRES8 (The user did not require code to preserve 8-byte aligment of 8-byte data objects)' function to 'REQ8 (Codewas permitted to depend on ...

    eac3to V3.17

    * when demuxing subtitle files, the number of captions is added to the filename * timestamp derived FPS is used for gap checking instead of video bitstream FPS * fixed: 44.1khz AC3 encoding was still ...

    Prototype Cheat Sheet

    - **`identify(element)`**: Assigns a unique identifier to an element. - **`immediateDescendants`**: Retrieves immediate child elements. - **`insert(element, {position: content})`**: Inserts content ...

    Odoo 11 Development Essentials 3 Edition(epub)

    We choose to install Odoo in an Ubuntu host, but guidance is given to have a perfectly functioning development environment in a Windows machine with an Ubuntu virtual machine. Chapter 3, Your First ...

    LTE from A to Z

    1.2.3.4.3 SFN for point to point services..............................................25 1.2.3.5 The Frequency Bands Intended for LTE.......................................26 1.2.3.5.1 Exclusive ...

    aes 加密代码

    of which is terminated by a single 0x00 octet. All extension identifiers are case sensitive. Examples of URIs: http://www.aescrypt.com/extensions/creator/ urn:oid:1.3.6.1.4.1.17090.55.14 urn:...

    CAN Specification Version 2.0

    applications has led to requirements that the assignment of message identifiers to communication functions be standardized for certain applications. These applications can be realized with CAN more ...

    留学生React Native计算器作业-待视频演示.zip

    The user can enter a number to each of the two textinput boxes, then click/tap either of the four images of mathematical operators. Once clicked, it should display a correct answer on the screen. 1. ...

    高性能消息传输机制Aeron.zip

    Can not go back to a wrapped value.Term Offset: Identifier of a single byte within the Term. Always start at 0. This is the number of the byte within a given term starting from the beginning.Frame...

    cucu-a compiler you can understand

    The primary objective of CUCU is to serve as an educational tool for those interested in understanding how compilers work. By creating a simplified version of a C compiler, the author aims to ...

Global site tag (gtag.js) - Google Analytics