I had posted an item about this a week or so ago:
http://forum.hibernate.org/viewtopic.php?t=966245&highlight=
In that post, i had a table with an identity key in sql server and a corresponding bean and mapping file. when I would try to save() (or saveOrUpdate() or merge() or persist()), I would get this error:
Quote:
org.hibernate.HibernateException: identifier of an instance of package.ProjectBean was altered from 1 to 0
I assumed the problem related to both Hibernate and SQL Server attempting to set the primary key value. So I changed my code and database schema. Now the primary key field in the database is simply an int, without any identity attributes. The generator in the mapping file for the primary key is increment, not identity. However, I still get the SAME problem. It appears that Hibernate is getting the new primary key value correctly, but in the org.hibernate.event.def.DefaultFlushEntityEventListener.checkId() method call that occurs, it appears that the primary key value for the bean has not been set, so the new key and old key values are not the same and saving is not permitted:
Code:
if ( persister.canExtractIdOutOfEntity() ) {
Serializable oid = persister.getIdentifier( object, entityMode );
if (id==null) {
throw new AssertionFailure("null id in " + persister.getEntityName() + " entry (don't flush the Session after an exception occurs)");
}
if ( !persister.getIdentifierType().isEqual(id, oid, entityMode) ) {
throw new HibernateException(
"identifier of an instance of " +
persister.getEntityName() +
" was altered from " + id +
" to " + oid
);
}
}
I believe that somewhere, Hibernate is not setting the primary key in the bean as it should, and this is causing the problem. As mentioned, this is a VERY basic example and I can't imagine what I am doing wrong. The code to test this is done in an JSP page. I didn't know if that could have anything to do with it.