Hi there...
I'm fairly new to hibernate and using Version 2.1.7. Unfortunately, I have a small problem that occurs when I'm trying to update the medicalRecordNumber (the PK of Patient). I googled around for more than an hour and had very little luck.
Lets assume that Patient contains the following fields (in reality it's huge and contains many other relationships):
medicalRecordNumber: java.lang.Integer
firstName: java.lang.String
lastName: java.lang.String
gender: java.lang.Boolean
Each patient has a unique medicalRecordNumber so I thought why not make that the identifier....
My partial hbm.xml:
Code:
<hibernate-mapping>
<class
name="org.clinic.Patient"
table="patients"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="medicalRecordNumber"
column="medicalRecordNumber"
type="java.lang.Integer"
unsaved-value="null">
<generator class="assigned">
</generator>
</id>
....
Notice that the identifier is assigned at creation of Patient.. however sometimes it is necessary for the end-users to modify the medicalRecordNumber of the patient.... lets say that a user already exists with mrn = 3 and it needs to be changed to 5... so the most obvious approach (within the same session):
Code:
Patient p = (Patient) session.load(Patient.class,new Integer(3));
p.setMedicalRecordNumber(new Integer(5));
session.flush();
session.close();
Unfortunately this gives the following exception:
Exception in thread "main" net.sf.hibernate.HibernateException: identifier of an instance of org.clinic.Patient altered from 3 to 5
I have tried many other things like splitting the sessions.. first one for loading the object and the second one for using session.update()... but then I get a problem that the row doesn't even exist...
I know people say that it is better to have the identifier independent of the bussiness candidate key, but it is way to late for that :(
What is the most efficient way to modify (update) the identifier in the same session (or not).
Many Thanks in Advance,
Saad