Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.3.0
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.paises, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)
at com.telvent.padron.bi.territorio.service.ConsuladoService.saveConsulado(ConsuladoService.java:109)
at com.telvent.padron.bi.territorio.service.ConsuladoService$$FastClassByCGLIB$$c70548f1.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:675)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:616)
at com.telvent.padron.bi.territorio.service.ConsuladoService$$EnhancerByCGLIB$$f2dfee95.saveConsulado(<generated>)
at com.telvent.padron.bi.territorio.service.ConsuladoServiceTest.testModify(ConsuladoServiceTest.java:153)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.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.java: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:228)
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.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.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_CONSULADO")}
)
@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.