Modelling Inheritance Bank with JPA

Asked

Viewed 292 times

0

I understand the concept of Heritage, currently I am working with a condo management project, and I find myself in the following question as you can see below;

inserir a descrição da imagem aqui

Please disregard the Debts table, my problem in question is the Condomino, Sindico, Resident and Proprietary table. The Syndic, Resident and Proprietary are a type of Condo, because I know that if I had to put the same attributes that are in Condomino and put in Sindico, Resident and Proprietary would be redundant, that’s why I put Condomino as Super-Class and Sindico classes, Resident and Owner as Sub-Class, that is, Condomino will be abstract and the other classes below will inherit the attributes of the Condomino class.

I know how to abstract all this very well in UML, and I also know how to do it in Java, in all classes I put JPA annotations as an example of @Notblank, and I only put the @Id, @Generatedvalue annotation in Sub-Classes because the Condomino class is an abstract class.

The other thing I did was just put @Entity in the Sub-Classes for the same reason the Condomino class is an abstract class.

Is that how I did it? Will when you run the project it will create my tables all straight in the base?

If even with the description I did below it is necessary to put the code here in this post, do not worry that I put, but I think with the descriptions I did above can understand.

2 answers

0

Normally in these cases we use Aggregation and not Inheritance. JPA implementations like Hibernate do not handle Inheritance very well. In practice, it would look something like this:

public class Condomino {
    // atributos omitidos
}

public class Sindico {
    // atributos omitidos

    @ManyToOne
    @JoinColumn(name = "condomino_id")
    private Condomino condomino;
}

For this case, the Condomino class would no longer be abstract.

Read more on: http://uaihebert.com/jpa-mini-livro-primeiros-passos-e-conceitos-detalhados/20/

  • What kind of problems could I have if I were to implement inheritance in my tables?

  • 1

    By default it will not work, you will need additional settings. Here is a post on the subject in more detail: http://blog.caelum.com.br/jpa-com-hibernate-heranca-e-mappings/

0

Implementing inheritance in jpa

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Condomino {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String nome,cidade,email;

    //todo getter setter

}

Classes inherited from Condomino

Resident

@Entity
public class Morador extends Condomino {
    private Date inicioAluguel;
    private Float taxaReajuste,valor;

}

Union

@Entity
public class Sindico extends Condomino{

    private Date inicioMandato,fimMadato;

}

Owner

@Entity
public class Proprietario extends Condomino {

    private Date dataCompra;
}

You have 3 strategies to implement inheritance

  • One table per class: @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  • Single table: @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  • Indexed tables: @Inheritance(strategy = InheritanceType.JOINED)

You choose according to your need:

  • If you want performance and use querys with polymorphism the most suitable strategy is Tabela única, but you run the risk of data inconsistency.

  • If you want consistency, performance and querys with polymorphism the best strategy is Indexação.

  • If you don’t need polymorphism or relationship querys the idea strategy is to Uma Tabela Por Classe, but keep in mind that you won’t be able to use too much polymorphism to create complex querys and this can generate loss of consistency

Browser other questions tagged

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