Quantcast
Channel: GUJ - Tópicos com a tag hibernate
Viewing all articles
Browse latest Browse all 759

[RESOLVIDO] Hibernate - Classe com herança e relacionamento

$
0
0

@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 N

package 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

Ler tópico completo


Viewing all articles
Browse latest Browse all 759

Trending Articles


Girasoles para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Tagalog Quotes About Crush – Tagalog Love Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Tagalog Quotes To Move on and More Love Love Love Quotes


5 Tagalog Relationship Rules


Best Crush Tagalog Quotes And Sayings 2017


Re:Mutton Pies (lleechef)


FORECLOSURE OF REAL ESTATE MORTGAGE


Sapos para colorear


tagalog love Quotes – Tiwala Quotes


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Patama Quotes : Tagalog Inspirational Quotes


Pamatay na Banat and Mga Patama Love Quotes


Tagalog Long Distance Relationship Love Quotes


BARKADA TAGALOG QUOTES


“BAHAY KUBO HUGOT”


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>