Arbitrary load of specialized entity in Java / Hibernate, from associations

1

So, following ... I have the following class structure that Hibernate handles:

is a symbol for denoting inheritance, 1 - * is a symbol for denoting an association one for many)

[Pessoa] <|- [Medico]

[Pessoa] <|- [Professor]

[Pessoa] 1 -- * [Contato] * -- 1 [TipoDeContato]

(For reinforcement effect, in the Contact entity there is an attribute that is Person type, so any instance of Physician, or Teacher, may have a relationship with any or several instances of Contact)

I have persisted the following objects (test only) (a table / class):

Pessoa: { id: 1, nome: "José Maria" }

Medico: { id: 1, crm: "0001" }

Professor: { id: 1, matricula: "20" }

TipoDeContato: { id: 2 , tipo: "email" }

Contato: { id: 2, pessoa: Pessoa { id: 1 }, tipoDeContato: TipoDeContato { id: 2}, contato: "jose@jose" }

Problem : When I load a contact, and access the content of the person attribute defined there (Contact), I imagined that it should return an instance of Person. Only not! It carries arbitrarily, to my mind, an instance of Doctor. Why does he carry an instance of Doctor? Why not load an instance of a Teacher, instead?!

SQL trace generated for the query by hibernate:

select * from
    public.contato this_ 
inner join
    public.pessoa pessoa2_ 
        on this_.pessoa_id=pessoa2_.id 
left outer join
    public.medico medico_1_ 
        on pessoa2_.id=medico_1_.id 
left outer join
    public.professor professor_1_ 
        on pessoa2_.id=professor_1_.id 
inner join
    public.tipodecontato tipodecont3_ 

Use java hibernate only. It's an exercise I'm doing for another job. When I went to evaluate inheritance behavior, I came across it. Weird! Help?

Looking at the Hibernate SQL for the query, I see it gives everyone a join: Person, Contact, Contact Type, and Physician, Teacher. How? !!

ps: I understand that quite possibly they can argue that doctor and patient are interfaces rather than entities. ok, but there are no cases where this can be evaluated as yes specialized entities?!

More,

public class Pessoa { public Long id; public String nome; }

public class Medico extends Pessoa { public String crm; }

public class Professor extends Pessoa { public String matricula; }

public class TipoDeContato { public Long id; public String tipo; }

public class Contato { public Long id; public Pessoa pessoa; public TipoDeContato tipoDeContato; public String contato; }
    
asked by anonymous 30.08.2016 / 19:20

0 answers