Bom dia pessoal! Estou querendo criar meu banco totalmente com hibernate, porém estou com problemas de Herança. Meu problema é o seguinte. Tenho a superclasse ‘Usuario’ e a subclasse ‘Paciente’.
Queria saber que anotações devo fazer nas classes, tanto na super quanto na sub, e se possível, como setar o valor da PK da superclasse para algum atributo/coluna da minha subclasse.
SUPERCLASSE
@Entity public class Usuario {
// Atributos
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_usuario")
private Long idUsuario;
@Column(length = 200, nullable = false)
private String nome;
@Column(columnDefinition = "char(9)", nullable = false, unique = true)
private String rg;
@Column(length = 11, nullable = false, unique = true)
private String cpf;
@Column(columnDefinition = "char(8)", length = 8, nullable = false, unique = true)
private String rne;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Temporal(TemporalType.DATE)
@Column(name = "data_nasc", nullable = false)
private Date dataNascimento;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "enum ('Masculino','Feminino')", nullable = false)
private Genero genero;
@Column(length = 255, nullable = false, unique = true)
private String email;
@Column(length = 32, nullable = false)
private String senha;
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "enum ('Medico','Paciente','Administrador')", nullable = false)
private TipoUsuario tipoUsuario;
@Column(name = "tel_residencial", columnDefinition = "int(10)")
private int telResidencial;
@Column(name = "tel_comercial", columnDefinition = "int(10)")
private Integer telComercial;
@Column(name = "tel_celular", columnDefinition = "bigint(11)")
private Integer telCelular;
@Column(name = "tel_recados", columnDefinition = "bigint(11)")
private int telRecados;
@Column(columnDefinition = "int(5)")
private Integer ramal;
@Column(name = "contato_recado", length = 100)
private String contatoRecado;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Endereco cep;
// Getters e Setters
public Long getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(Long idUsuario) {
this.idUsuario = idUsuario;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getRne() {
return rne;
}
public void setRne(String rne) {
this.rne = rne;
}
public Date getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Date dataNascimento) {
this.dataNascimento = dataNascimento;
}
public Genero getGenero() {
return genero;
}
public void setGenero(Genero genero) {
this.genero = genero;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public TipoUsuario getTipoUsuario() {
return tipoUsuario;
}
public void setTipoUsuario(TipoUsuario tipoUsuario) {
this.tipoUsuario = tipoUsuario;
}
public Integer getTelResidencial() {
return telResidencial;
}
public void setTelResidencial(Integer telResidencial) {
this.telResidencial = telResidencial;
}
public Integer getTelComercial() {
return telComercial;
}
public void setTelComercial(Integer telComercial) {
this.telComercial = telComercial;
}
public Integer getTelCelular() {
return telCelular;
}
public void setTelCelular(Integer telCelular) {
this.telCelular = telCelular;
}
public Integer getTelRecados() {
return telRecados;
}
public void setTelRecados(Integer telRecados) {
this.telRecados = telRecados;
}
public Integer getRamal() {
return ramal;
}
public void setRamal(Integer ramal) {
this.ramal = ramal;
}
public String getContatoRecado() {
return contatoRecado;
}
public void setContatoRecado(String contatoRecado) {
this.contatoRecado = contatoRecado;
}
public Endereco getCep() {
return cep;
}
public void setCep(Endereco cep) {
this.cep = cep;
}
}
SUBCLASSE@Entity public class Paciente extends Usuario {
// Atributos
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPaciente;
@Column(columnDefinition = "tinyint(1)", nullable = false)
private Integer convenio;
@Column(columnDefinition = "mediumblob")
private byte[] foto;
// Getters e Setters
public Long getIdPaciente() {
return idPaciente;
}
public void setIdPaciente(Long idPaciente) {
this.idPaciente = idPaciente;
}
public Integer getConvenio() {
return convenio;
}
public void setConvenio(Integer convenio) {
this.convenio = convenio;
}
public byte[] getFoto() {
return foto;
}
public void setFoto(byte[] foto) {
this.foto = foto;
}
// Converte os bytes em base 64 String
public String getFoto64() {
String encodedImage = Base64.encode(foto);
return encodedImage;
}
}
Só estou passando a entidade e subclasse Paciente para nao ficar grande o texto.
Desde já, agradeço!