Doubt about Hibernate heritage

Asked

Viewed 190 times

0

I have 3 classes / 2 tables: employee / secretary :

Functionary

salesman employee extends

secretariat extends staff

i wanted to make a single table inheritance with seller and employee

and an inheritance joined with secretary and employee it is possible to do this?

classe funcionario;

@Entity(name="Funcionario")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "tipo", length = 1, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("F")
@Table(name = "funconarios")
public abstract class Funcionario implements Autenticar {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "codFuncionario")
    private String codFuncionario;

salesman:

@Entity
@DiscriminatorValue(value = "V")
public class Vendedor extends Funcionario implements BonusSalario{
}

and how I would do in the secretarial class?

public class Secretaria extends Funcionario implements BonusSalario{
}
  • Dude, I think it’s best you think of composition, px: Seller has work data (ai vc can write it down with @Embeded) class Seller { private Funcionario funcionario}

  • @But like the seller class I wouldn’t even need to extend because it will have the same attributes as it works and the same table, you could answer with an example using this @embededso I can better understand how to use it in the manager class?

  • I think using only JOINED should solve, except that in the case would be created 3 tables, where the primary employee key would be the foreign key of the children, the common attributes the seller and secretary would be in the abstract class

  • https://memorynotfound.com/hibernate-jpa-joined-table-inheritance-example/

1 answer

0


If I understand correctly, this mapping NAY would have the right sense, because:

In Single table with seller and employee would generate a single table:

  • Employee table: containing all information together seller/employee

And the joined with secretary and employee would have 2 tables:

  • Employee table: only with employee information;
  • Desk table: with desk information and a FOREIGN KEY to the employee table

If it is possible to make this mixture in this way the secretary would have in the mapping in the table internal information of the seller, example of what would be the mapping in summary and visual form:

Exemplo do mapeamento

From my point of view the right thing would be to use the JOINED in both mappings

Joined

Reference JOINED table: Hibernate Docs

  • in which case I would have 3 tables in the bank / seller/ secretary / employee is not?

  • Yes, actually in your case I think it would fit @Mappedsuperclass correctly Hibernate Docs I’ll edit the answer later

Browser other questions tagged

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