-->
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.  [ 4 posts ] 
Author Message
 Post subject: Extending persisted class with non persisted variables
PostPosted: Wed Aug 15, 2007 4:35 pm 
Newbie

Joined: Wed Aug 15, 2007 3:50 pm
Posts: 6
Location: Philadelphia
I have a class SocPatient.java with a corresponding table Soc_Patient and a hibernate mapping file SocPatient.hbm.xml.

I extended this class in order to add two non-persisted booleans (which I only need to keep around for JSPs) in the following fashion:

Code:
public class SocPatientChild extends SocPatient {
   private static final long serialVersionUID = ....;
   private boolean validationErrorsPresent;
   private boolean conclusionErrorsPresent;
   ...
}

, and then remapped all formerly used SocPatients to use SocPatientChild wherever needed.

Now when I run the application, I keep getting MappingExceptions on SocPatientChild when a previous calls to save the SocPatients were being executed.

I tried a few different things:

1) created a SocPatientChild.hbm.xml mapped to table Soc_Patient with the same content as SocPatient.hbm.xml (failed with some other exception)

2) upcasted SocPatientChild to SocPatient before attempting the SAVE
e.g. (SocPatient) socPatientChild

3) got desperate and tried cloning (made no difference!)

In the end, I managed to solve the problem by using a combination of upcasting and copy constructor, BUT I WONDER IF THERE'S A BETTER WAY:
Code:
public class SocPatientChild extends SocPatient {
  ...
  public SocPatient getSocPatient(){
            SocPatient temp = new SocPatient(this);
            return temp;
  }
}


where the SocPatient copy constructor is:
public class SocPatient implements PatientResult,Serializable,java.lang.Cloneable{
  ...
  public SocPatient(SocPatient toCopy) {
      if (toCopy !=null){
              this.admissionDate = toCopy.getAdmissionDate();
              ... manually copied everything else...
      }
  }
}

Thanks for any help,
-melina


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 5:38 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
At the point you save the object you can tell hibernate what you want it saved as like this:

Code:
SocPatientChild spc = new SocPatientChild();
session.save("com.melinas.SocPatient", spc);


As these additional fields are solely for you jsp it might be worth considering creating a separate class that _contains_ a SocPatient and the extra fields rather than extending SocPatient.


Top
 Profile  
 
 Post subject: going with first solution
PostPosted: Thu Aug 16, 2007 10:12 am 
Newbie

Joined: Wed Aug 15, 2007 3:50 pm
Posts: 6
Location: Philadelphia
Solution #1: I like this solution, that's exactly what I was trying to do (but through upcasting which didn't work).

Solution #2: including SocPatient inside the SocPatientChild rather than extending it would've been a bit more intrusive since it would've required remapping all of the functionality to add the extra socPatientchild.getSocPatient() call in order to access SocPatient's members.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 10:26 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You're right about #2. One way around this is to create delegate methods which map to the contained object. Eclipse for example has an automated delegate generator which does the job for you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.