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

后端 未结 17 2416
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: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);
    }  
    

提交回复
热议问题