Hibernate: How to fix “identifier of an instance altered from X to Y”?

后端 未结 17 2381
org.hibernate.HibernateException: identifier of an instance 
of org.cometd.hibernate.User altered from 12 to 3

in fact, my user table

相关标签:
17条回答
  • 2020-12-01 07:48

    In my case, the PK Field in hbm.xml was of type "integer" but in bean code it was long.

    0 讨论(0)
  • 2020-12-01 07:52

    It is a problem in your update method. Just instance new User before you save changes and you will be fine. If you use mapping between DTO and Entity class, than do this before mapping.

    I had this error also. I had User Object, trying to change his Location, Location was FK in User table. I solved this problem with

    @Transactional
    public void update(User input) throws Exception {
    
        User userDB = userRepository.findById(input.getUserId()).orElse(null);
        userDB.setLocation(new Location());
        userMapper.updateEntityFromDto(input, userDB);
    
        User user= userRepository.save(userDB);
    }  
    
    0 讨论(0)
  • 2020-12-01 07:54

    I solve this by instancing a new instance of depending Object. For an example

    instanceA.setInstanceB(new InstanceB());
    instanceA.setInstanceB(YOUR NEW VALUE);
    
    0 讨论(0)
  • 2020-12-01 07:56

    Problem can be also in different types of object's PK ("User" in your case) and type you ask hibernate to get session.get(type, id);.

    In my case error was identifier of an instance of <skipped> was altered from 16 to 32. Object's PK type was Integer, hibernate was asked for Long type.

    0 讨论(0)
  • 2020-12-01 07:57

    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!

    0 讨论(0)
提交回复
热议问题