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

Hibernate Lista Duplicate entry

$
0
0

@Jeanderson_Silva escreveu:

Fala galera. Bom estou precisando da ajuda de vocês, Eu tenho vários CURSOS cadastrado no banco de dados. ao fazer uma matricula de um ALUNO eu pego o Cursos que ele selecionou e coloca em uma List dentro desse aluno e salvo no banco de dados. A treta é o seguinte quando eu faço uma instanciar de aluno, e coloco o curso dentro da List deste aluno, na primeira vez vai, mas ao instanciar outro aluno e colocar o mesmo curso que já foi adicionado ao outro aluno, dar o seguinte error:

ERROR: Duplicate entry '1' for key 'UK_79s4gn9ouad4ayxcj5bo8x0bh'

Então pelo o que eu vejo eu não posso colocar na List do outro aluno um curso q já esteja na List de outro aluno, isso não entendi galera. o modelo de relacionamento é este:

Minha classe Aluno e Curso:

@Entity
@Table(name = "alunos")
public class Aluno implements Serializable {

    private final IntegerProperty id;
    private final StringProperty nome;
    private final StringProperty cpf;
    private final StringProperty rg;
    private final StringProperty telefone;
    private final StringProperty celular;
    private final StringProperty endereco;
    private final StringProperty bairro;
    private final StringProperty cidade;
    private final StringProperty uf;
    private LocalDate dataNascimento;
    private LocalDate dataMatricula;
    private Funcionario funcionarioMatricula;
    private List<Curso> cursos;

    public Aluno() {
        this.id = new SimpleIntegerProperty();
        this.nome = new SimpleStringProperty();
        this.cpf = new SimpleStringProperty();
        this.rg = new SimpleStringProperty();
        this.telefone = new SimpleStringProperty();
        this.celular = new SimpleStringProperty();
        this.endereco = new SimpleStringProperty();
        this.bairro = new SimpleStringProperty();
        this.cidade = new SimpleStringProperty();
        this.uf = new SimpleStringProperty();
        this.cursos = new ArrayList<>();

    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "aluno_id")
    public Integer getId() {
        return id.get();
    }

    @Column(name = "nome", length = 40)
    public String getNome() {
        return nome.get();
    }

    @Column(name = "cpf", length = 20, unique = true)
    public String getCpf() {
        return cpf.get();
    }

    @Column(name = "rg", length = 15, unique = true)
    public String getRG() {
        return this.rg.get();
    }

    @Column(name = "telefone", length = 16)
    public String getTelefone() {
        return telefone.get();
    }

    @Column(name = "celular", length = 16)
    public String getCelular() {
        return celular.get();
    }

    @Column(name = "endereco", columnDefinition = "text")
    public String getEndereco() {
        return endereco.get();
    }

    @Column(name = "bairro", length = 50)
    public String getBairro() {
        return this.bairro.get();
    }

    @Column(name = "cidade", length = 50)
    public String getCidade() {
        return this.cidade.get();
    }

    @Column(name = "uf", length = 4)
    public String getUf() {
        return this.uf.get();
    }

    @Column(name = "data_nascimento")
    public LocalDate getDataNascimento() {
        return this.dataNascimento;
    }

    @Column(name = "data_matricula")
    public LocalDate getDataMatricula() {
        return this.dataMatricula;
    }

    @ManyToOne
    @JoinColumn(name = "funcionario_id")
    public Funcionario getFuncionarioMatricula() {
        return funcionarioMatricula;
    }

    @OneToMany()
    @JoinTable(name = "aluno_cursos", joinColumns = @JoinColumn(name = "aluno_id"), inverseJoinColumns = @JoinColumn(name = "curso_id"))
    public List<Curso> getCursos() {
        return this.cursos;
    }
  //demais sets ocultados
}

Classe Curso:

@Entity
@Table(name = "cursos")
public class Curso implements Serializable {
    
    private final IntegerProperty id;
    private final StringProperty nome;
    private final IntegerProperty cargaHoraria;
    private final StringProperty conteudo;
    private final DoubleProperty valor;
    private final IntegerProperty porcentagemDesconto;
    
    
    public Curso() {
        this.id = new SimpleIntegerProperty();
        this.nome = new SimpleStringProperty();
        this.cargaHoraria = new SimpleIntegerProperty();
        this.conteudo = new SimpleStringProperty();
        this.valor = new SimpleDoubleProperty();
        this.porcentagemDesconto = new SimpleIntegerProperty();
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "curso_id")
    public Integer getId() {
        return id.get();
    }
    
    @Column(name = "nome", length = 40, nullable = false)
    public String getNome() {
        return nome.get();
    }
    
    @Column(name = "carga_horaria")
    public Integer getCargaHoraria() {
        return cargaHoraria.get();
    }   
        
    @Column(name = "conteudo", columnDefinition = "text")
    public String getConteudo() {
        return this.conteudo.get();
    }
    @Column(name = "valor", columnDefinition = "DECIMAL(10,2)")
    public double getValor(){
        return this.valor.get();
    }
    @Column(name = "porcentagem_desconto",columnDefinition = "INT DEFAULT '0'")
    public int getPorcentagemDesconto(){
        return this.porcentagemDesconto.get();
    }
  //demais sets ocultados.
}

e a maneira que estou salvando um aluno no banco é assim:

Aluno aluno = new Aluno();
aluno.setNome(txtNome.getText().trim());
//tabela que já contém os cursos. foi populada com os cursos no banco de dados
aluno.setCursos(tbCursoSelecionado.getItems());
//salva o aluno.
BancoDeDados.save(aluno);

alguém poderia me dar uma Luz? pois só salva o primeiro aluno, mas se salvar outro aluno q tenha o curso tbm, já dar aquele erro.

Mensagens: 2

Participantes: 1

Ler tópico completo


Viewing all articles
Browse latest Browse all 759

Trending Articles


Sapos para colorear


UPDATE SC IDOL: TWO BECOME ONE


Pokemon para colorear


Presence Quotes – Positive Quotes


The business quotes | Inspirational and Motivational Quotes for you


Two timer Sad tagalog Love quotes


“BAHAY KUBO HUGOT”


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


EASY COME, EASY GO


FORECLOSURE OF REAL ESTATE MORTGAGE


HOY PANGIT, MAGBAYAD KA!


Girasoles para colorear


Love Quotes Tagalog


Long Distance Relationship Tagalog Love Quotes


Top 10 Best “Single” Tagalog Love Quotes


“Mali man na ikaw ay ibigin ko, akoy iibig padin sayo”


RE: Mutton Pies (frankie241)


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>