-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Problem with many-to-many association
PostPosted: Thu Aug 23, 2007 2:44 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 8:10 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
hi

you can try this :


Code:
public class Consulado extends Descriptora implements IAuditable, Serializable
{

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 9:59 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
It does work if I put fetch.eager, but I need it to be lazy.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 9:59 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
It does work if I put fetch.eager, but I need it to be lazy.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 10:04 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
It does work if I put fetch.eager, but I need it to be lazy.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 10:54 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
If lazy, you must do a query on the database for get the list with objects from class pais


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 10:59 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
Not really,

"Lazy collection fetching - a collection is fetched when the application invokes an operation upon that collection. (This is the default for collections.) "


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 11:04 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
I found the solution:

Hibernate does not support lazy initialization for detached objects. The fix is to move the code that reads from the collection to just before the transaction is committed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 11:04 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
only if the Hibernate session is already open, and your log say :
Quote:
no session or session was closed


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 5:46 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
Hi again,
After doing some more testing, I have noticed that I does not work when running the test,but it does work when I do a debug.
Moreover, if I do the test more than one time with the same consulado, it only brings the paises the first time I do the debug, the second time I get an empty list.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.