Arbitrary Java/Hibernate Specialized Entity Load from Associations

Asked

Viewed 50 times

1

So, next ... I have the following class structure that treated by Hibernate:

( <|- is a symbol to denote inheritance, 1 --* is a symbol to denote an association one for many)

[Pessoa] <|- [Medico]

[Pessoa] <|- [Professor]

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

(For reinforcement, in the entity Contact there is an attribute that is of the type Person. Thus, any instance of Doctor, or Teacher, may have a relationship with none 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 carry a contact, and access the content of the attribute person defined there (Contact), I imagined that it should return an instance of Person. Just not! It charges arbitrarily, it seems to me, an instance of Doctor. Why does he carry an instance of Doctor? Because it does not carry a Teacher instance, instead?!

SQL trace generated for Hibernate query:

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 only. is an exercise I’m doing for another job. When I went to evaluate inheritance behavior, I came across this. Strange! Helps?

Observing the SQL of Hibernate for the query, I see that it gives a Join worldwide: Person, Contact, Contact and Doctor, Professor. How?!!

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

plus,

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; }
  • what type of Strategy SINGLE_TABLE or JOINED ?

  • @Dilneicunha, is that what you want to know? @Inheritance(Strategy = Inheritancetype.JOINED)

  • @Dilneicunha in each of the entities I put in the specialization tree I define in the @Inheritance class statement(Strategy = Inheritancetype.JOINED). Is that it? Plus, at the bank, I have one table per class

  • I figured, since this is the default behavior of the JOINED type, and these joins are the price to pay for using inheritance, but the biggest problem is that you need a discriminator, which your bank driver?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.