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

后端 未结 17 2380
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<Customer> 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);
    }
    
    0 讨论(0)
  • 2020-12-01 07:40

    In my case getters and setter names were different from Variable name.

    private Long stockId;
        public Long getStockID() {
            return stockId;
        }
        public void setStockID(Long stockID) {
            this.stockId = stockID;
        }
    

    where it should be

    public Long getStockId() {
        return stockId;
    }
    public void setStockId(Long stockID) {
        this.stockId = stockID;
    }
    
    0 讨论(0)
  • 2020-12-01 07:41

    Are you changing the primary key value of a User object somewhere? You shouldn't do that. Check that your mapping for the primary key is correct.

    What does your mapping XML file or mapping annotations look like?

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

    I was facing this issue, too.

    The target table is a relation table, wiring two IDs from different tables. I have a UNIQUE constraint on the value combination, replacing the PK. When updating one of the values of a tuple, this error occured.

    This is how the table looks like (MySQL):

    CREATE TABLE my_relation_table (
      mrt_left_id BIGINT NOT NULL,
      mrt_right_id BIGINT NOT NULL,
      UNIQUE KEY uix_my_relation_table (mrt_left_id, mrt_right_id),
      FOREIGN KEY (mrt_left_id)
        REFERENCES left_table(lef_id),
      FOREIGN KEY (mrt_right_id)
        REFERENCES right_table(rig_id)
    );
    

    The Entity class for the RelationWithUnique entity looks basically like this:

    @Entity
    @IdClass(RelationWithUnique.class)
    @Table(name = "my_relation_table")
    public class RelationWithUnique implements Serializable {
    
      ...
    
      @Id
      @ManyToOne
      @JoinColumn(name = "mrt_left_id", referencedColumnName = "left_table.lef_id")
      private LeftTableEntity leftId;
    
      @Id
      @ManyToOne
      @JoinColumn(name = "mrt_right_id", referencedColumnName = "right_table.rig_id")
      private RightTableEntity rightId;
    
      ...
    

    I fixed it by

    // usually, we need to detach the object as we are updating the PK
    // (rightId being part of the UNIQUE constraint) => PK
    // but this would produce a duplicate entry, 
    // therefore, we simply delete the old tuple and add the new one
    final RelationWithUnique newRelation = new RelationWithUnique();
    newRelation.setLeftId(oldRelation.getLeftId());
    newRelation.setRightId(rightId);  // here, the value is updated actually
    entityManager.remove(oldRelation);
    entityManager.persist(newRelation);
    

    Thanks a lot for the hint of the PK, I just missed it.

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

    In my case it was because the property was long on object but int in the mapping xml, this exception should be clearer

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

    You must detach your entity from session before modifying its ID fields

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