1 Reply Latest reply on Nov 5, 2009 5:02 AM by Nikolay Elenkov

    Send mail and LazyInitializationException

    Wagner Santos Newbie

      I'm trying send e-mail with seam-mail, but I'm getting LIE when do this. Somebody can help me? See the code:

      public void enviaEmailEAtualiza(Prazo prazo) {
                final Map<String, Object> objetosContextuais = new HashMap<String, Object>();
                objetosContextuais.put("prazo", prazo);
                objetosContextuais.put("processo", prazo.getProcesso());
                //I do this to try eliminate the problem
                objetosContextuais.put("advogadoGestor", prazo.getProcesso().getAdvogadoGestor());
                objetosContextuais.put("advogadoEmpresa", prazo.getProcesso().getAdvogadoEmpresa());
                asynchronousMailProcessor.envia(10000, "/mail/PrazoVencido.xhtml", objetosContextuais);

      In template:

      <m:from name="Não responda"
                address="sender@novosrumosdesenvolvimento.com.br" />
           <m:to name="#{advogadoGestor.nome}">#{advogadoGestor.email}</m:to>
           <m:cc name="#{advogadoEmpresa.nome}">#{advogadoEmpresa.email}</m:cc> ...

      And, then, I'm get LIE:

      ERROR: [05/11/2009 - 00:47:28] could not initialize proxy - no Session
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session
           at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
           at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
           at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
           at br.com.nrdp.juriprev.entity.administracao.Usuario_$$_javassist_10.getEmail(Usuario_$$_javassist_10.java)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      Thanks for any help and sorry for my bad English.

        • 1. Re: Send mail and LazyInitializationException
          Nikolay Elenkov Master

          You need to make sure all related entities and fields are loaded before you pass your entity to your asynchronous method.
          Looks like you are getting an error on Usario.getEmail(), is the email field lazily loaded? If som, make sure you access it (or better yet, join fetch it) before you kick off the async method. Try something in the lines of:

          objetosContextuais.put("advogadoGestorEmail", prazo.getProcesso().getAdvogadoGestor().getEmail);

          and then use it in your template:

          <m:to name="#{advogadoGestor.nome}">#{advogadoGestorEmail}</m:to>