Modeling bank with Inheritance with JPA

0

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

Please disregard the Debts table, my problem in question is the Condomino, Syndicate, Dweller and Owner table. The Sindico, Morador and Proprietario are a type of Condomino, because I know that if I had to put the same attributes that are in Condomino and put in Sindico, Morador and Proprietario would be redundant, that's why I put Condomino as Super-Class and the classes Sindico, Dweller 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 can abstract all this very well in UML, and I also know how to do this in Java, in all classes I put JPA annotations such as @NotBlank, and I just 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 class Condomino is an abstract class.

Does it shape what I did is correct? Is it when I run the project it will create all my tables right in the database?

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

    
asked by anonymous 10.07.2016 / 12:38

2 answers

0

Usually in these cases we use Aggregation and not Inheritance. Implementations of JPA such as Hibernate do not deal very well with Inheritance. 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 not be more abstract.

Read more at: link

    
10.07.2016 / 14:40
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 inheriting from Condomino

Dweller

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

}

Syndicate

@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 best 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 do not need polymorphism or relationship queries, the idea strategy is Uma Tabela Por Classe , but keep in mind that you will not be able to use a lot of polymorphism to create complex queries, p>

20.04.2018 / 22:58