-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: identifier of an instance of Patient altered from 3 to 5
PostPosted: Tue Jan 11, 2005 1:25 am 
Newbie

Joined: Tue Jan 11, 2005 1:01 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 11, 2005 6:44 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
and id is not mutable, you should consider using a surrogate key

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.