Heritage Java web Inheritance

Asked

Viewed 330 times

0

I have the inheritance to do below and would like to know how it would look on main. Someone here could help?

Could you tell me if you have any problems with the mapping I did and how mainTeste would look? I cannot define the value of the Lancamentodesparcelado class...

inserir a descrição da imagem aqui

I use: 1º the Eclipse; 2nd Oracle database and to view the database tables use oracle SQL Developer;
3º And in the project we will use the strategy of multiple tables with use of Join; I’ll put the beginning of the classes:

RELEASE

 @Entity
 @Inheritance(strategy = InheritanceType.JOINED)
 @DiscriminatorColumn(NAME="TIPO", discriminatorType=DiscriminatorType.CHAR)
 @Table(name="LANCAMENTO")
 public class Lancamento extends GenericModel{ ... }

LANCAMENTO_DESPESA

@Entity
public class LancamentoDespesa extends Lancamento{ ... }

LANCAMENTO_RECEITA

@Entity
public class LancamentoReceita extends Lancamento{ ... }

LANCAMENTO_DESPESA_PARCELADA

@Entity
public class LancamentoDespesa extends LancamentoDespesa{ 
    @Column(name = "PARCELA", nullable = true)
    private int numParcela;

    GET/SET...
}

Final remarks: In SQL Developer creates inheritance tables, but also creates 4 other tables with names:

HT_LANCAMENTO  
HT_LANCAMENTODESPESA  
HT_LANCAMENTORECEITA  
HT_LANCAMENTODESPESAPARCELADA  

And all have in the drawing of the table a little box with x.

I can’t get another picture in here if I wouldn’t.

1 answer

1


Rafael, I copied the code and made the following changes for the generation of tabels to work:

RELEASE

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(NAME="TIPO", discriminatorType=DiscriminatorType.INTEGER)
@Table(name="LANCAMENTO")
public class Lancamento extends GenericModel { ... }

LANCAMENTO_RECEITA

@Entity
@DiscriminatorValue("1")
public class LancamentoReceita extends Lancamento { ... }

LANCAMENTO_DESPESA

@Entity
@DiscriminatorValue("2")
public class LancamentoDespesa extends Lancamento { ... }

LANCAMENTO_DESPESA_PARCELADA

@Entity
@DiscriminatorValue("3")
public class LancamentoDespesa extends LancamentoDespesa { 
    @Column(name = "PARCELA", nullable = true)
    private int numParcela;

    GET/SET...
}

When using @Inheritance(strategy = InheritanceType.JOINED) and @DiscriminatorColumn, it is also necessary to use the annotation @DiscriminatorValue to define the value that will be used to distinguish the specific entity type.

In my example I changed to DiscriminatorType.INTEGER because using CHAR did not work with Hibernate in my tests.

  • And what would be the main part of the unitary test to launch LANCAMENTO? How do I launch the plots of the LANCAMENTODESPARCELADA class?

  • Lancamento lancamento = new Lancamentodespesaparcelada(); service.save(lancamento);

  • Yes. I did it, okay, but at what point do I define the plots? intende... ai que ficou perdido. This system is for financial launches of REVENUE or EXPENSE, and correct me if I’m wrong... I WON’T instantiate LANCAMENTO, I always have to instantiate and choose LANCAMENTO_DESPESA or LANCAMENTO_RECEITA or if it is installments LANCAMENTO_DESPESA_PARCELADA, this PQ I will have everything LANCAMENTO and in case if installments, I have INSTALLMENT... this is the reasoning... sorry if I’m wrong.

  • Try it now, and that’s it, Roque. In SQL Developer in the LANCAMENTO table I have all data and in the TYPE column I have 3 (from the LANCAMENTODESPARCELADA table). And in the table LANCAMENTODESPESAPARCELADA I have the id of the LANCAMENTO and the value of plots of the column PARCELA = 10.

  • Cool. If I helped you, please mark the answer as correct :)

  • I forgot that for example when we have PERSON and we have TRAINEE and EMPLOYEE who inherit from PERSON, we never INSTANCE PERSON and yes INTERN or EMPLOYEE. Helped yes!

Show 1 more comment

Browser other questions tagged

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