@thimfont escreveu:
Olá galera,
tudo bem?Por favor, poderiam me ajudar com o seguinte cenário. Tenho dúvida sobre como modelá-lo?
Tenho dois atores para o sistema:
- Gerente
- Representante
Onde 1 Gerente é responsável por muitos Representantes.
Como ambos possuem os mesmos atributos, criei uma classe abstrata chamada Usuario e herdo ela nas classes Gerente e Representante. Também criei um classe ENUM chamada Perfil.
O que preciso é, criar um gerente. Em outro momento, criar um representante e relacioná-lo com um gerente existente mas não estou conseguindo este resultado. Seguem minhas classes:
Enum PERFIL
public enum Perfil { REPRESENTANTE, GERENTE; }
Classe USUARIO
package com.roche.planotatico.model; import org.hibernate.annotations.Type; import javax.persistence.*; import javax.validation.constraints.NotNull; import java.util.Calendar; @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @TableGenerator(name = "USUARIO_GEN", table = "USUARIO_ID", pkColumnName = "ID", pkColumnValue = "USUARIO", valueColumnName = "VALOR", allocationSize = 1) public abstract class Usuario { @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "USUARIO_GEN") private Long id; @NotNull private String nome; @NotNull private String email; @Type(type = "true_false") private boolean status; @Temporal(TemporalType.DATE) private Calendar dataDeCatastro; @Temporal(TemporalType.DATE) private Calendar dataDoUltimoAcesso; @Enumerated(EnumType.STRING) private Perfil perfil; ... gets() e sets() ... }
Classe REPRESENTANTE
package com.roche.planotatico.model; import javax.persistence.*; @Entity @Table(name = "Representante") public class Representante extends Usuario { }
Classe GERENTE
Nela estou tentando criar o relacionamento 1 para Npackage com.roche.planotatico.model; import javax.persistence.*; import java.util.List; @Entity @Table(name = "Gerente") public class Gerente extends Usuario { @OneToMany private List<Representante> representantes; public List<Representante> getRepresentantes() { return representantes; } public void setRepresentantes(List<Representante> representantes) { this.representantes = representantes; } }
O resultado que estou tendo atualmente é:
- uma tabela para controlar numeração dos IDs
- uma tabela para Representante;
- uma tabela para Gerente;
- uma tabela associativa gerente_representante - mas nela, o Id do gerente é uma chave primária, ou seja, não me deixa cadastrar um outro usuário para o mesmo gerente.
Por favor, poderiam me ajudar? Qual a boa prática de modelagem neste cenário?
Obrigado.
Abraço.
Mensagens: 6
Participantes: 3