Instantiate annotated class with Hibernate inheritance using CDI

Asked

Viewed 286 times

2

Good afternoon,

I’m creating a project using JSF, CDI, Bootstrap and Hibernate.

I would like to know how to work with the following problem, I have created a set of classes to represent a person entity física or jurídica. The structure was as follows:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="tipo", discriminatorType=DiscriminatorType.STRING, length=1)
@Named("clientePojo")
public abstract class Cliente extends Persistent {

    public enum TipoCliente {

        FISICA('F', "Física"), 
        JURIDICA('J', "Juridica");

        private final char tipo;
        private final String descricao;

        private TipoCliente(char tipo, String descricao) {
            this.tipo = tipo;
            this.descricao = descricao;
        }

        public static TipoCliente valueOf(char sigla) {

            switch (sigla) {
            case 'F':
                return TipoCliente.FISICA;
            case 'J':
                return TipoCliente.JURIDICA;
            default:
                return null;
            }
        }

        public char getTipo() {
            return tipo;
        }

        public String getDescricao() {
            return descricao;
        }
    }

    @Column(insertable=false, updatable=false)
    private String tipo;

    // getter e setter  
}

@Entity
@DiscriminatorValue(value="F")
public class ClienteFisico extends Cliente {

    private String nome;

    private Date dataNascimento;

    private String identidade;

    private String cpf;

    // getter e setter
}

@Entity
@DiscriminatorValue(value="J")
public class ClienteJuridico extends Cliente {

    private String razaoSocial;
    private String nomeFantasia;
    private String cnpj;

    // getter e setter
}

How should I treat this case to instantiate in bean? Since the registration form you select whether the type is physical or legal.

1 answer

2

Recover if the type of person to be registered by the form is Physical or Legal and install an object of the type you want.

Ex.:

if (TipoCliente.FISICO == form.getTipoPessoaValue()) {
   ClienteFisico cliente = new ClienteFisico();
   // insira seus setters aqui
}

You can instantiate an object from a daughter class, and even then set attributes that are represented by the superclass (as in the case of Client and Clientephysics).

Obs.: It is recommended that you do not manipulate data from entities directly. For this, use Data Transfer Objects or DTO’s. You would have something like "Clientedto" and etc.

  • Dude, I’m going to change the project and I’m going to separate the classes. But I wonder if this is going to be a problem in jsf

  • Another question is when I instate the object, because it would not give pal if I do a validation or something with ajax no?

  • Daria would. Whatever component library you are using with JSF (primefaces or richfaces), you have element attributes that are triggered asynchronously. I suggest you take a look at the documentation of the library component you are using. It will be a piece of cake ;)

  • I’ll test and any doubt I’ll tell you!

  • Could you post in the reply the example of DTO as it would please

  • You only create a class identical to the Clientefisico, however, with the name Clientephysicodto. This new DTO class will not be annotated by @Entity or any other class. It will only serve as a representation of your Clientefisico. Where you retrieve the client type (physical or legal) and instance of a new Clientephysic, you must instill a Clientephysicodate and right after your DTO object is populated with the data, then you set the attributes of that DTO object to the Entity (Clientephysic). I’ll put the example of the code in a little bit if you can’t. (I’m by cell phone rs).

Show 1 more comment

Browser other questions tagged

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