Capturar exceção gerada pelo hibernate

6 respostas
crpablo

Fala galerinha…

To com um problema pra capturar um exceção gerada pelo hibernate…

A exception é a seguinte:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed

Já fui até a linha onde tá sendo gerada essa exceção, tentei tratar ela, mas não tem jeito…

Ela não encerra o programa nem altera os dados que estou manipulando… porém, fica akelas coisas vermelhas no console me encomodando…

Alguém tem uma idéia?

[]'s

6 Respostas

albertoleal

Parece ser erro no seu mapeamento…

posta seus xml’s ou suas classes anotadas ai

crpablo

Fala ae Alberto...

Então deixa eu explicar onde está ocorrendo a exception...

O mapeamento tá funcando legal... a persistência tá sendo executada sem problema nenhum....

Essa excessão tá sendo gerada quando eu vou criar meus xml's...

Eu faço uma consulta no bd, pego todos os dados da tabela e mando gerar o xml... quando eu gero o xml ele dispara essa exceção.

Dá uma olhada no cód.

Pega os dados e manda gerar o xml:
dao.init();

		List<Proxy> proxyList = dao.findAll();
		SerizaliaObj serializa = new SerizaliaObj();
		serializa.criaXML("PROXY.xml", proxyList);

		dao.close();
Gera o xml(Aqui gera a exception LINHA 03):
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
					new FileOutputStream("xml/" + nomeArquivo)));
			encoder.writeObject(objetos); //NESSA LINHA DISPARA A EXCEÇÃO
			encoder.close();

Acho que é isso...

albertoleal

existe alguma herança para esses objetos que vc está tentando serializar em xml?

crpablo
Cara... dá uma olhada na entity...
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Proxy extends CiaEntity {

	private static final long serialVersionUID = 8243270071058613501L;

	private String leitura = "";

	private String escrita = "";

	private String trap = "";

	private boolean emailHabilitado = false;
	
	private boolean habilitarKeepAlive = false;

	private long intervaloKeepAlive = 0;

	private Set<EnderecoIp> enderecoIp = new HashSet<EnderecoIp>();

	// Geral
	private long intervaloAmostragem = 5000;

	private int[] controleAcesso = new int[4];

	// Syslog
	private boolean habilitarSyslog = false;

	private boolean anexarHostName = false;

	private int[] servidorSyslog = new int[4];

	// Email

	private String servidorSmtp = "";

	private String de = "";

	private String para = "";

	private String cc = "";

	private String assunto = "";

	private String msg = "";

	public String getAssunto() {
		return assunto;
	}

	public void setAssunto(String assunto) {
		this.assunto = assunto;
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getDe() {
		return de;
	}

	public void setDe(String de) {
		this.de = de;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getPara() {
		return para;
	}

	public void setPara(String para) {
		this.para = para;
	}

	public String getServidorSmtp() {
		return servidorSmtp;
	}

	public void setServidorSmtp(String servidorSmtp) {
		this.servidorSmtp = servidorSmtp;
	}

	public boolean isAnexarHostName() {
		return anexarHostName;
	}

	public void setAnexarHostName(boolean anexarHostName) {
		this.anexarHostName = anexarHostName;
	}

	public int[] getControleAcesso() {
		return controleAcesso;
	}

	public void setControleAcesso(int[] controleAcesso) {
		this.controleAcesso = controleAcesso;
	}

	public boolean isHabilitarSyslog() {
		return habilitarSyslog;
	}

	public void setHabilitarSyslog(boolean habilitarSyslog) {
		this.habilitarSyslog = habilitarSyslog;
	}

	public long getIntervaloAmostragem() {
		return intervaloAmostragem;
	}

	public void setIntervaloAmostragem(long intervaloAmostragem) {
		this.intervaloAmostragem = intervaloAmostragem;
	}

	public int[] getServidorSyslog() {
		return servidorSyslog;
	}

	public void setServidorSyslog(int[] servidorSyslog) {
		this.servidorSyslog = servidorSyslog;
	}

	public Proxy() {

	}

	@Id 
    @GeneratedValue(generator="sequence")
    @GenericGenerator(name="sequence", strategy = "increment")
	public Long getId() {
		return super.getId();
	}

	@OneToMany(targetEntity = EnderecoIp.class)
	@Cascade( { CascadeType.ALL })
	@LazyCollection(value = LazyCollectionOption.FALSE)
	public Set<EnderecoIp> getEnderecoIp() {
		return enderecoIp;
	}

	public void setEnderecoIp(Set<EnderecoIp> enderecoIp) {
		this.enderecoIp = enderecoIp;
	}

	public String getEscrita() {
		return escrita;
	}

	public void setEscrita(String escrita) {
		this.escrita = escrita;
	}

	public boolean isHabilitarKeepAlive() {
		return habilitarKeepAlive;
	}

	public void setHabilitarKeepAlive(boolean habilitarKeepAlive) {
		this.habilitarKeepAlive = habilitarKeepAlive;
	}

	public long getIntervaloKeepAlive() {
		return intervaloKeepAlive;
	}

	public void setIntervaloKeepAlive(long intervaloKeepAlive) {
		this.intervaloKeepAlive = intervaloKeepAlive;
	}

	public String getLeitura() {
		return leitura;
	}

	public void setLeitura(String leitura) {
		this.leitura = leitura;
	}

	public String getTrap() {
		return trap;
	}

	public void setTrap(String trap) {
		this.trap = trap;
	}




	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (anexarHostName ? 1231 : 1237);
		result = prime * result + ((assunto == null) ? 0 : assunto.hashCode());
		result = prime * result + ((cc == null) ? 0 : cc.hashCode());
		result = prime * result + Arrays.hashCode(controleAcesso);
		result = prime * result + ((de == null) ? 0 : de.hashCode());
		result = prime * result + (emailHabilitado ? 1231 : 1237);
		result = prime * result
				+ ((enderecoIp == null) ? 0 : enderecoIp.hashCode());
		result = prime * result + ((escrita == null) ? 0 : escrita.hashCode());
		result = prime * result + (habilitarKeepAlive ? 1231 : 1237);
		result = prime * result + (habilitarSyslog ? 1231 : 1237);
		result = prime * result
				+ (int) (intervaloAmostragem ^ (intervaloAmostragem >>> 32));
		result = prime * result
				+ (int) (intervaloKeepAlive ^ (intervaloKeepAlive >>> 32));
		result = prime * result + ((leitura == null) ? 0 : leitura.hashCode());
		result = prime * result + ((msg == null) ? 0 : msg.hashCode());
		result = prime * result + ((para == null) ? 0 : para.hashCode());
		result = prime * result
				+ ((servidorSmtp == null) ? 0 : servidorSmtp.hashCode());
		result = prime * result + Arrays.hashCode(servidorSyslog);
		result = prime * result + ((trap == null) ? 0 : trap.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Proxy other = (Proxy) obj;
		if (anexarHostName != other.anexarHostName)
			return false;
		if (assunto == null) {
			if (other.assunto != null)
				return false;
		} else if (!assunto.equals(other.assunto))
			return false;
		if (cc == null) {
			if (other.cc != null)
				return false;
		} else if (!cc.equals(other.cc))
			return false;
		if (!Arrays.equals(controleAcesso, other.controleAcesso))
			return false;
		if (de == null) {
			if (other.de != null)
				return false;
		} else if (!de.equals(other.de))
			return false;
		if (emailHabilitado != other.emailHabilitado)
			return false;
		if (enderecoIp == null) {
			if (other.enderecoIp != null)
				return false;
		} else if (!enderecoIp.equals(other.enderecoIp))
			return false;
		if (escrita == null) {
			if (other.escrita != null)
				return false;
		} else if (!escrita.equals(other.escrita))
			return false;
		if (habilitarKeepAlive != other.habilitarKeepAlive)
			return false;
		if (habilitarSyslog != other.habilitarSyslog)
			return false;
		if (intervaloAmostragem != other.intervaloAmostragem)
			return false;
		if (intervaloKeepAlive != other.intervaloKeepAlive)
			return false;
		if (leitura == null) {
			if (other.leitura != null)
				return false;
		} else if (!leitura.equals(other.leitura))
			return false;
		if (msg == null) {
			if (other.msg != null)
				return false;
		} else if (!msg.equals(other.msg))
			return false;
		if (para == null) {
			if (other.para != null)
				return false;
		} else if (!para.equals(other.para))
			return false;
		if (servidorSmtp == null) {
			if (other.servidorSmtp != null)
				return false;
		} else if (!servidorSmtp.equals(other.servidorSmtp))
			return false;
		if (!Arrays.equals(servidorSyslog, other.servidorSyslog))
			return false;
		if (trap == null) {
			if (other.trap != null)
				return false;
		} else if (!trap.equals(other.trap))
			return false;
		return true;
	}

	public boolean isEmailHabilitado() {
		return emailHabilitado;
	}

	public void setEmailHabilitado(boolean emailHabilitado) {
		this.emailHabilitado = emailHabilitado;
	}

}
albertoleal

Pablo, nunca vi essa exeção.

Procurando no site do hibernate, encontrei isso:

http://www.hibernate.org/43.html

crpablo

Blz Alberto…

Vou dar mais uma olhada aqui…

Se conseguir resolver, posto aqui…

Abraço

Criado 4 de março de 2008
Ultima resposta 4 de mar. de 2008
Respostas 6
Participantes 2