Sunday, 4 August 2013

Fix for identifier of an instance of was altered from [parameters... = values...] to null

Recently while using hibernate with java and Mysql i got this error.

java.lang.RuntimeException: identifier of an instance of <class> was altered from PrimaryKeyClass [parameters... = values...] to null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

The error was simple to resolve but time taking to figure out.

The mistake that i made was in the setter for the primary key.

I had the code as :

private void setPk(Pk pk)
{
    pk = pk;
}

While the correct code should be :

private void setPk(Pk pk)
{
    this.pk = pk;
}

so if you get a error like this don't forget to take a closer look at the setters too.