0

I have a JPA/Hibernate entity which uses a composite key as the primary key. When I read the object from the database and make an update I get the error

javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of navigation.external.CustomerNavigationType was altered from navigation.external.CustomerNavigationTypeId@7ccdbbc5 to navigation.external.CustomerNavigationTypeId@bba8c505

The error happens when I flush.

I have read several other stackoverflow posts and tried to implement the suggestions, but I still get the error. Can you see what I am missing?

Entity:

@NoArgsConstructor
@Getter
@Setter
@Entity(name = "CustomerNavigationType")
@IdClass(CustomerNavigationTypeId.class)
@Table(name = "MY_TABLE")
public class CustomerNavigationType {

    @Id
    @Column(name = "BANKNR")
    private BigDecimal bankNr;

    @Id
    @Column(name = "NAVTYPE")
    private String navType;

    @Id
    @Column(name = "JUREN")
    private String juren;

    @Column(name = "STATU")
    private String state;
}

CompositeKey:

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class CustomerNavigationTypeId implements Serializable {

    private BigDecimal bankNr;

    private String navType;

    private String juren;

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        CustomerNavigationTypeId other = (CustomerNavigationTypeId) obj;
        return Objects.equals(bankNr, other.getBankNr())
            && Objects.equals(navType, other.getNavType())
            && Objects.equals(juren, other.juren());
    }

    @Override
    public int hashCode() {
        return Objects.hash(bankNr, navType, juren);
    }
}

DatabaseClient code:

EntityManager em = entityManagerProducer.getEntityManager();
CustomerNavigationTypeId compositeId = new CustomerNavigationTypeId(
   BigDecimal.valueOf(1),
   "NAVRULE1",
   "2021-08-05 22:15:00.384395");

em.getTransaction().begin();
CustomerNavigationType customerNavigationType = em.find(CustomerNavigationType.class, compositeId);
customerNavigationType.setState("ACTIVE");
em.flush();
em.getTransaction().commit();
1
  • I don't exactly what happens. But why do you use BigDecimal for the bankNr? Aug 9, 2021 at 6:03

1 Answer 1

0

I solved the issue by using @EmbeddedId and @Embeddable instaead of @Idclass etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.