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

后端 未结 17 2436
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:38

    Make sure you aren't trying to use the same User object more than once while changing the ID. In other words, if you were doing something in a batch type operation:

    User user = new User();  // Using the same one over and over, won't work
    List customers = fetchCustomersFromSomeService();
    for(Customer customer : customers) {
     // User user = new User(); <-- This would work, you get a new one each time
     user.setId(customer.getId());
     user.setName(customer.getName());
     saveUserToDB(user);
    }
    

提交回复
热议问题