Question

I am new to Hibernate and learning all the different annotations can be frustrating at times. Currently, I am stuck on making Doctor extend Person and also have a 1-to-1 relationship between Doctor and Specialty. I've been stuck on this for a while and still cannot figure this one out. I've tried testing out one of the two relationships and my code works fine, but I encounter a problem when I put everything together.

this is the error I'm getting:

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: edu.cs157b.medicalSystem.Specialty, at table: Person, for columns: [org.hibernate.mapping.Column(specialty)]

Doctor:

package edu.cs157b.medicalSystem;

import javax.persistence.*;

@Entity

public class Doctor extends Person {

    @OneToOne
    @JoinColumn(name = "SPECIALTY_ID")
    private Specialty specialty;

    private double salary;



    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSpecialty(Specialty specialty) {
        this.specialty = specialty;
    }

    public Specialty getspecialty() {
        return specialty;
    }
}

Speciality:

package edu.cs157b.medicalSystem;

import javax.persistence.*;

@Entity

public class Specialty {

    @OneToOne
    private Doctor doctor;

    @Id
    @GeneratedValue
    @Column(name = "SPECIALTY_ID")
    private int sId;

    private String specialtyTitle;

    public void setSId(int sId) {
        this.sId = sId;
    }

    public int getSId() {
        return sId;
    }

    public void setSpecialtyTitle(String specialtyTitle) {
        this.specialtyTitle = specialtyTitle;
    }

    public String getSpecialtyTitle() {
        return specialtyTitle;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Doctor getDoctor() {
        return doctor;
    }
}

Person:

package edu.cs157b.medicalSystem;
import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

public class Person {

    private int personId;
    private String first_name;

    public Person() {

    }

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "PERSON_ID")
    public int getPersonId() {
        return personId;
    }

    public void setPersonId(int personId){
        this.personId = personId;
    }

    public void setFirstName(String first_name) {
        this.first_name = first_name;
    }

    public String getFirstName() {
        return first_name;
    }

}
Was it helpful?

Solution

There are two errors in your code.

First, you annotated the getter in Person, and annotated the field in its subclass Doctor. That's why you get this error: once Hibernate sees the @Id annotation on a getter in the base class, it only considers annotations on getters in the rest of the class hierarchy, and ignores the annotations on fields.

Second, your OneToOne bidirectional association is mapped incorrectly. One side must always be the inverse side in a bidirectional association. So, the following field:

@OneToOne
private Doctor doctor;

should be

@OneToOne(mappedBy = "specialty")
private Doctor doctor;

to inform JPA that the Specialty.doctor association is the inverse side of the OneToOne association already declared and mapped in Doctor.specialty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top