Persistencia com hibernate e list

5 respostas
PePeLeGaL

alguem sabe o motivo do seguinte erro no codigo abaixo ?

tx.begin();
                Aluno aluno = new Aluno();
		
		aluno.setMatricula(12846822);
		aluno.setNome("Joao Maria Costa Neto");
		
		aluno.setTelefones(new ArrayList<String>());
		aluno.getTelefones().add("32154268");
		
		session.save(aluno);
		
		tx.commit();
		
		session.close();
package br.com.marcel.dominio;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;

@Entity
@Table(name="aluno", schema="anotacoes")
public class Aluno 
{
	
	private int id;
	private int matricula;
	private String nome; 
	private long cpf;
	
	@CollectionOfElements
	@JoinTable(name="aluno_telefone", schema="anotacoes", joinColumns = @JoinColumn(name="id_aluno"))
	@Column(name="telefone")
	@IndexColumn(name="posicao")
	private List<String> telefones;
	
	@Id
	@SequenceGenerator(name="seqAluno", sequenceName="SEQ_TESTE",  allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqAluno")
	@Column(name="id_aluno")
	public int getId() 
	{
		return id;
	}
	public void setId(int id) 
	{
		this.id = id;
	}
	
	public int getMatricula() 
	{
		return matricula;
	}
	public void setMatricula(int matricula) 
	{
		this.matricula = matricula;
	}
	
	public String getNome() 
	{
		return nome;
	}
	public void setNome(String nome) 
	{
		this.nome = nome;
	}
	
	public long getCpf() 
	{
		return cpf;
	}
	public void setCpf(long cpf) 
	{
		this.cpf = cpf;
	}
	/**
	 * @return the telefones
	 */
	public List<String> getTelefones() {
		return telefones;
	}
	/**
	 * @param telefones the telefones to set
	 */
	public void setTelefones(List<String> telefones) {
		this.telefones = telefones;
	}
}
package br.com.marcel.dominio;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;

public class AlunoTelefone 
{
	@Column(name="id_telefone")
	@SequenceGenerator(name="seqUniversidade", sequenceName="SEQ_ALUNO_TELEFONE",  allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqUniversidade")
	private int id;
	
	private String telefone;
	
	private int posicao;
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the telefone
	 */
	public String getTelefone() {
		return telefone;
	}

	/**
	 * @param telefone the telefone to set
	 */
	public void setTelefone(String telefone) {
		this.telefone = telefone;
	}

	/**
	 * @return the posicao
	 */
	public int getPosicao() {
		return posicao;
	}

	/**
	 * @param posicao the posicao to set
	 */
	public void setPosicao(int posicao) {
		this.posicao = posicao;
	}
}

5 Respostas

crpablo

Tentou passar uma lista de telefones ao invés de uma lista te strings?

aluno.setTelefones(new ArrayList&lt;String&gt;()); aluno.getTelefones().add("32154268");

PePeLeGaL

ja tentei sim. Fiz da forma abaixo e todas as alteracoes nas classes, mas o erro continuou

Aluno aluno = new Aluno();
		
		aluno.setMatricula(12846822);
		aluno.setNome("Joao Maria Costa Neto");
		
		aluno.setTelefones(new ArrayList<AlunoTelefone>());
		
		AlunoTelefone al = new AlunoTelefone();
		al.setTelefone("32154244");
		
		aluno.getTelefones().add(al);
		
		session.save(aluno);
PePeLeGaL

com Set nao adiantou, pois aconteceu erro semelhante.

gerdec

Boa tarde, pelo erro citado e pela ideia que tive, passar um arraylist para um campo de uma tabela não parece ser valido para o hibernate tente passar apenas um parametro e veja se o erro persiste espero ter ajudado

Spool

utiliza Set ao invés de List

@CollectionOfElements   
    @JoinTable(name="aluno_telefone", schema="anotacoes", joinColumns = @JoinColumn(name="id_aluno"))   
    @Column(name="telefone")   
    @IndexColumn(name="posicao")   
    private Set<String> telefones;
Criado 11 de fevereiro de 2008
Ultima resposta 11 de fev. de 2008
Respostas 5
Participantes 4