Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate inheritance

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.

like image 695
Jef Avatar asked Feb 24 '12 15:02

Jef


People also ask

What is Hibernate inheritance?

Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass. Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

Does Hibernate support inheritance?

Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.

Which inheritance strategy is better in Hibernate?

If you have a single hierarchy, prefer To use Single Table Inheritance strategy. Joined strategy is better designed when you have a complex hierarchy but it suffers due To performance issues when querying for any entity. Save this answer.

How many strategies are there in Hibernate inheritance?

4 Inheritance Strategies. JPA and Hibernate support 4 inheritance strategies which map the domain objects to different table structures.


2 Answers

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?

like image 196
pap Avatar answered Oct 05 '22 22:10

pap


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.

like image 45
Jeff Goldberg Avatar answered Oct 05 '22 23:10

Jeff Goldberg