Unable to persist fields in subclass

#1

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)

#2

The getMedicalRecordNumber and dateOfBirth persistent properties are not defined properly.

A property requires definition of both set and get methods.

ObjectDB Support
#3

Thanks you! That worked beautifully.

David

Reply