5

I am working on a project with hibernate. I got these two files:

Persoon.java

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "PROJ_TYPE")
@Table(name = "PROJECT")
public class Persoon {

    @Id
    @GeneratedValue
    protected int persoonsnummer;
    protected String voornaam;
    protected String achternaam;
    protected String adres;
    protected String geboortedatum;
    protected String telefoonnummer;
    protected String titel;

    // Getters and setters ommited for brevity
}

Patient.java

@Entity
@DiscriminatorValue("P")
@Table(name="PATIENT")
public class Patient extends Persoon implements Serializable {   

    protected String allergieen;
    protected String bijzonderheden;

    // Getters and setters ommited for brevity
}

And Test.java to fill the tables:

public class Test {
    public static void main(String[] args) {
        // get a session
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        // Start a tx
        session.beginTransaction();
        // create a person
        Persoon i = new Persoon();
        i.setVoornaam("Henk");
        i.setAchternaam("Oliebol");
        i.setAdres("1234AA Rotterdam");
        i.setGeboortedatum("10-10-1990");
        i.setTelefoonnummer("012345678");
        i.setTitel("Patient");
        session.save(i);

        // create a patient
        Patient p = new Patient();
        p.setAllergieen("geen");
        p.setBijzonderheden("geen");
        session.save(p);// create another car

        // commit the tx, 
        // so that it will be visible in the db
        session.getTransaction().commit();
    }
}

I want patient to inherit persoon, what is the way of solving this?

I.e. After making a persoon, I want to connect patient to persoon.

3
  • Do you really need to inherit patient here? Wouldn't just to define a OneonOne relationship be better? Commented Feb 24, 2012 at 15:37
  • you want two different things Commented Feb 24, 2012 at 15:37
  • Patient already extends Persoon. What do you want exactly? What do you mean by "connect"? Commented Feb 24, 2012 at 15:49

2 Answers 2

9

I generally discourage people from using inheritance/polymorphism in Hibernate and this is a prime example why. What added value are you getting from creating this inheritance that outweighs the complexity it generates? Wouldn't it be easier to just have one entity for Person and one for Patient, and make Patient contain a person, i.e have a M-1 relationship with a person?

Keep it simple, especially with Hibernate. Relational databases are not meant to handle inheritance, they are meant to handle relationships so why force it into something it's not meant for?

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with you generally, but I don't think it's fair to say one should never do it this way. Hibernate does support this kind of inheritance for a reason, and there are always cases where risking a little complexity with Hibernate will save a great deal of complexity elsewhere.
@JeffGoldberg - "For a reason" is a strong assumption, especially with something that is open-source at it's core. Just as likely, it's there because one of the contributors simply wanted to implement it for the sake of implementing it. But sure, there are certain to be situations where it could be useful. I can't think of one right now, I must confess, but yeah.
I actually happen to use it and like it a lot. The main scenario is that I have that the parent is NEVER used alone... It is always used with the child. In this case, it is a blessing that it can auto create/update/delete the parent as if they were one.
4

You are missing the annotation

@PrimaryKeyJoinColumn(name="persoonsnummer")

from your Patient class.

This tells Hibernate that your person table has a persoonsnummer column which serves as a unique id and that the patient table also has a persoonsnummer which serves to link a patient row to the appropriate person row.

Note, if in your schema you do NOT have a persoonsnummer column on the patient table (with the exact same definition as the corresponding column on the person table) you will need to add one.

Also, in order to create a new Patient using Hibernate you should NOT have to also create a Person object. The Patient class extends Person, so you can set all the fields for both Patient and Person directly on it. When you use Hibernate to save the Patient it will create both a patient and person row in the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.