Announcement Announcement Module
Collapse
No announcement yet.
Lazy initialization problem in a manytomany association Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy initialization problem in a manytomany association

    Hi,
    When I run my applicationīs test I get the following exception:

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.telvent.padron.bi.territorio.model.Consulado.p aises, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:358)
    at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:350)
    at org.hibernate.collection.AbstractPersistentCollect ion.initialize(AbstractPersistentCollection.java:3 43)
    at org.hibernate.collection.AbstractPersistentCollect ion.read(AbstractPersistentCollection.java:86)
    at org.hibernate.collection.PersistentBag.iterator(Pe rsistentBag.java:249)
    at com.telvent.padron.bi.territorio.service.Consulado Service.saveConsulado(ConsuladoService.java:109)
    at com.telvent.padron.bi.territorio.service.Consulado Service$$FastClassByCGLIB$$c70548f1.invoke(<genera ted>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy. java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$C glibMethodInvocation.invokeJoinpoint(Cglib2AopProx y.java:675)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :154)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:107)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :176)
    at org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:616)
    at com.telvent.padron.bi.territorio.service.Consulado Service$$EnhancerByCGLIB$$f2dfee95.saveConsulado(< generated>)
    at com.telvent.padron.bi.territorio.service.Consulado ServiceTest.testModify(ConsuladoServiceTest.java:1 53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:164 )
    at junit.framework.TestCase.runBare(TestCase.java:130 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:110)
    at junit.framework.TestResult.runProtected(TestResult .java:128)
    at junit.framework.TestResult.run(TestResult.java:113 )
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 28)
    at junit.framework.TestSuite.run(TestSuite.java:223)
    at org.junit.internal.runners.OldTestClassRunner.run( OldTestClassRunner.java:35)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)



    The funny part itīs that when I run the testīs debug, it works some times, but everytime I get a different loadof the collection. For example, I get an empty list and other times I get an element.

    The code of classes are:


    /*Consulado*/


    public class Consulado extends Descriptora implements IAuditable, Serializable
    {

    .....
    .....
    .....
    @ManyToMany
    (
    mappedBy="consulados",
    cascade={CascadeType.PERSIST, CascadeType.MERGE},
    targetEntity=Pais.class
    )
    ......
    ......
    ......
    }

    /*Pais*/
    public class Pais extends Descriptora implements Serializable
    {

    ......
    ......
    ......
    @ManyToMany
    (
    cascade={CascadeType.PERSIST, CascadeType.MERGE},
    targetEntity=Consulado.class
    )
    @JoinTable(
    name="TRR_PAIS_CONSULADO",
    joinColumns={@JoinColumn(name="NIDEN_PAIS")},
    inverseJoinColumns={@JoinColumn(name="NIDEN_CONSUL ADO")}
    )
    @ForeignKey(name="FK_PAISCONSULADO_PAIS")
    private List<Consulado> consulados;
    ......
    ......
    ......
    }

    /* JpaConsuladoDao */



    /**
    * Obtiene el objeto actualizado con los datos directos de la base de datos
    * evitando la sesion de hibernate.
    *
    * @param numerero
    * @return
    */
    public Consulado getConsuladoFisico(Consulado objetoActual) {
    logger.debug("getConsuladoFisico - Start");
    boolean esta=false;
    //Si no sacamos el objeto actual de la session da error el refresh
    getHibernateTemplate().evict(objetoActual);
    //Recargamos de base de datos el objeto de nuevo
    Consulado objetoFisico = new Consulado();
    objetoFisico.setId(objetoActual.getId());
    getHibernateTemplate().refresh(objetoFisico);
    //Sacamos el objeto de Fisico de la session para al actualizar el id no de error al salvar.
    getHibernateTemplate().evict(objetoFisico);
    //Lo he sacado de sesion y lo vuelvo a meter para que no me de problemas el lazy
    if(getHibernateSession().contains(objetoActual))
    getHibernateSession().lock(objetoActual, LockMode.NONE);
    return objetoFisico;
    }


    The object objetoFisico should be able to obtain a list with objects from class pais. But it doesnīt. Later on when I try to access such info
    it does not work.
Working...
X