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.
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
– Macario1983
Another question is when I instate the object, because it would not give pal if I do a validation or something with ajax no?
– Macario1983
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 ;)
– andreybleme
I’ll test and any doubt I’ll tell you!
– Macario1983
Could you post in the reply the example of DTO as it would please
– Macario1983
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).
– andreybleme