516 words

Unable to persist fields in subclass

#1
2017-01-11 14:06

I have a superclass Person, with several fields

I have a subclass Patient with additional fields.

I can persist Patient, but CANNOT persist the fields in this subclass. Why is this?

example code:

package miscTests;
 
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
 
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.MappedSuperclass;
 
/**
* @author david
*
*/
 
@MappedSuperclass
@Access(AccessType.PROPERTY)
public abstract class SimpleAbstractPersonTest {
 
    private long addressID;
    private long contactID;
 
    // Use Properties to allow integration with JavaFX
 
    protected StringProperty lastName = new SimpleStringProperty();
 
    public SimpleAbstractPersonTest() {}
 
    public String getLastName() {
        return lastName.get();
    }
 
    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }
 
    public StringProperty lastNameProperty() {
        return lastName;
    }
 
    public final long getAddressID() {
            return addressID;
        }
 
     public final void setAddressID(long id) {
      if (id > 0) addressID = id;
        }
 
     public final long getContactID() {
             return contactID;
         }
 
      public final void setContactID(long id) {
       if (id > 0) contactID = id;
         }
}
 
package miscTests;
 
import java.time.LocalDate;
 
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
 
/**
* @author david
*
*/
 
@Entity
@Access(AccessType.PROPERTY)
public class SimplePatientTest extends SimpleAbstractPersonTest {
 
/**
  *
  */
 
@Id @GeneratedValue private long id;
 
private int medicalRecordNumber;
 
//Using java.time rather than deprecated java.sql
private ObjectProperty<LocalDate> dateOfBirth = new SimpleObjectProperty<LocalDate>();
    //eg: (1959, Month.DECEMBER, 12);
 
public SimplePatientTest(int mrn) {
  super();
  this.medicalRecordNumber = mrn;
}
 
public long getId() {
  return id;
}
 
@Access(AccessType.PROPERTY) public int getMedicalRecordNumber() {
  return medicalRecordNumber;
}
 
@Access(AccessType.PROPERTY) public LocalDate getDateOfBirth () {
  return dateOfBirth.get();
}
 
void publicSetDateOfBirth(LocalDate date) {
  if (dateOfBirth == null) {
   dateOfBirth  = new SimpleObjectProperty<LocalDate>(date);
  } else dateOfBirth.set(date);
}
 
    public ObjectProperty<LocalDate> dateOfBirth() {
        return dateOfBirth;
    }
 
}
 
package miscTests;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
public class SimpleTestSuite {
 
public static void main(String[] args) {
 
  EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("objectdb:simplePatientTest.tmp;drop");  //;drop
   EntityManager em = emf.createEntityManager();
 
  SimplePatientTest patient = new SimplePatientTest(1248675);
  patient.setLastName("Hogg");
 
  em.getTransaction().begin();
        em.persist(patient);
        em.getTransaction().commit();
 
        em.close();
        emf.close();
 
    }
}

Using Explorer, I can see the schema contains: addressID, contactID, lastName (all from superclass)

but not: medicalRecordNumber, dateOfBirth (from subclass)

dhogg
dhogg's picture
Joined on 2016-12-31
User Post #1
#2
2017-01-11 14:51

The getMedicalRecordNumber and dateOfBirth persistent properties are not defined properly.

A property requires definition of both set and get methods.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,681
#3
2017-01-12 00:39

Thanks you! That worked beautifully.

David

dhogg
dhogg's picture
Joined on 2016-12-31
User Post #2

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel