org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:

142 views
Skip to first unread message

Ray da Costa

unread,
Apr 21, 2009, 8:56:26 PM4/21/09
to


Uma ajuda.. aguem sabe como fazer pare recuperar meu objeto sem gerar este error; org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:


Ao tentar executar o metodo findById gera o error acima...


MEU EJB

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ClienteEJBBean implements ClienteEJBRemote,ClienteEJBLocal {


@PersistenceContext
private EntityManager em;
//EntityTransaction tx = null;



public ClienteEJBBean(){

}

public ClienteEntity salvar(String idEmpresa,ClienteEntity objeto) throws AplicacaoTesteException {
try{
if(objeto.getId() == null){
objeto.setIdObjeto(GetID.GETID());
objeto.setDataCriado(new Date());
objeto.setIdEmpresa(idEmpresa);
this.em.persist(objeto);
}else{
this.em.merge(objeto);
}


} catch (Exception e) {
objeto = null;
e.printStackTrace();
throw new AplicacaoTesteException("ERRO NO SESSION FINDBYID [Cliente] "+e.toString());
}
return objeto;
}


@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public ClienteEntity findById(String idEmpresa,long id) throws AplicacaoTesteException {
ClienteEntity objeto = null;
try {

String SQL_ID_OBJETO = "select * from AplicacaoTeste_ClienteEntity where id = "+id+" and idEmpresa = '"+idEmpresa+"' ";
Query q = em.createNativeQuery(SQL_ID_OBJETO,com.raysystem.jdavincci.aplicacaoteste.model.ClienteEntity.class);
objeto = (ClienteEntity)q.getSingleResult();

} catch (Exception e) {
e.printStackTrace();
//throw new AplicacaoTesteException("ERRO NO SESSION FINDBYID [Cliente] "+e.toString());
}
return objeto;
}

public List<ClienteEntity> findByIdSuperior(String idEmpresa,long idObjeto) throws AplicacaoTesteException {
List<ClienteEntity> lista = new ArrayList<ClienteEntity>();
try {
String SQL_ID_OBJETO = "select * from AplicacaoTeste_ClienteEntity where IdObjeto = '"+idObjeto+"' and idEmpresa = '"+idEmpresa+"' ";
Query q = em.createNativeQuery(SQL_ID_OBJETO,com.raysystem.jdavincci.aplicacaoteste.model.ClienteEntity.class);
lista = q.getResultList();

} catch (Exception e) {
e.printStackTrace();
//throw new AplicacaoTesteException("ERRO NO SESSION IDSUPERIOR [Cliente] "+e.toString());
}
return lista;
}


public List<ClienteEntity> findAll(String idEmpresa) throws AplicacaoTesteException{
List<ClienteEntity> lista = new ArrayList<ClienteEntity>();
try {
String SQL_ID_OBJETO = "select * from AplicacaoTeste_ClienteEntity where idEmpresa = '"+idEmpresa+"' ";
Query q = em.createNativeQuery(SQL_ID_OBJETO,com.raysystem.jdavincci.aplicacaoteste.model.ClienteEntity.class);
lista = q.getResultList();

} catch (Exception e) {
e.printStackTrace();
//throw new AplicacaoTesteException("ERRO NO SESSION FIND ALL [Cliente] "+e.toString());
}
return lista;
}


public ClienteEntity delete(String idEmpresa,long id) throws AplicacaoTesteException {
ClienteEntity objeto = this.findById(idEmpresa,id);
try {
if(objeto != null){
this.em.remove(objeto);
}
} catch (Exception e) {
e.printStackTrace();
throw new AplicacaoTesteException("ERRO NO SESSION DELETE [Cliente] "+e.toString());
}
return objeto;
}


}


MEU ENTITY

@Entity
@Table(name="AplicacaoTeste_ClienteEntity")
public class ClienteEntity implements Serializable{


private static final long serialVersionUID = 1L;
// Atributo criado pela ferramenta JDavincci

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY )
private List<ContatoEntity> contato = null;//new ArrayList<ContatoEntity>();

@Column
private String idEmpresa = null;

@Column
private String idObjeto = null;

@Column
private int tipoPessoa = 0;

@Column
private String nome = null;

@Column
private String descricao = null;

@Column
private String inscr = null;

@Temporal(javax.persistence.TemporalType.DATE)
private Date dataCriado = null;

@Column
private String status = null;

@Column
private String cpf = null;



@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id = null;

public ClienteEntity(){

}

public void setContato(List<ContatoEntity> objeto) {
this.contato = objeto;
}


@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public List<ContatoEntity> getContato(){
return this.contato;
}

public void setIdEmpresa(String objeto) {
this.idEmpresa = objeto;
}


public String getIdEmpresa(){
return this.idEmpresa;
}

public void setIdObjeto(String objeto) {
this.idObjeto = objeto;
}


public String getIdObjeto(){
return this.idObjeto;
}

public void setTipoPessoa(int objeto) {
this.tipoPessoa = objeto;
}


public int getTipoPessoa(){
return this.tipoPessoa;
}

public void setNome(String objeto) {
this.nome = objeto;
}


public String getNome(){
return this.nome;
}

public void setDescricao(String objeto) {
this.descricao = objeto;
}


public String getDescricao(){
return this.descricao;
}

public void setInscr(String objeto) {
this.inscr = objeto;
}


public String getInscr(){
return this.inscr;
}

public void setDataCriado(Date objeto) {
this.dataCriado = objeto;
}


public Date getDataCriado(){
return this.dataCriado;
}

public void setStatus(String objeto) {
this.status = objeto;
}


public String getStatus(){
return this.status;
}

public void setCpf(String objeto) {
this.cpf = objeto;
}

Ray da Costa
www.ray.system.nom.br

Maurício Linhares

unread,
Apr 21, 2009, 11:48:57 PM4/21/09
to pb...@googlegroups.com
Tá faltando um pedaço da mensagem do seu erro aí, mas o erro pode
provavelmente ser resolvido alterando esse código:

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY )
private List<ContatoEntity> contato = null;//new
ArrayList<ContatoEntity>();

Pra esse:

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER )
private List<ContatoEntity> contato = null;//new
ArrayList<ContatoEntity>();

Mas fazer isso obviamente também tem os seus problemas.

É melhor você procurar ler a documentação ou um bom livro sobre
JPA/HIbernate ( http://www.manning.com/bauer2/ ) porque o seu código
contém falhas graves, como a concatenação de strings na hora de montar
consultas em JPA-QL.

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
Reply all
Reply to author
Forward
0 new messages