[Spring][Hibernate]illegal access to loading collection
Bonjour,
Je bloque actuellement sur l'accès aux données dans un projet de développement Spring/Hibernate. L'application sera lancée directement en ligne de commande.
L'environnement du test qui ne passe pas est le suivant :
applicationContext.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="mappingResources">
<list>
<value>/test/Message.hbm.xml</value>
<value>/test/object.hbm.xml</value>
</list>
</property>
</bean>
<bean name="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="objectDao" class="object.dao.objectDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="objectDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"><ref bean="objectDaoTarget"/></property>
<property name="proxyInterfaces">
<value>dao.object.objectDao</value>
</property>
<property name="interceptorNames">
<list><value>hibernateInterceptor</value></list>
</property>
</bean> |
Mapping de object :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<hibernate-mapping>
<class name="data.object.objectBean"
table="object">
<id name="objectId" type="long" column="OBJECT_ID">
<generator class="increment"></generator>
</id>
<set name="messages" cascade="all">
<key column="OBJECT_ID"></key>
<one-to-many class="data.message.MessageBean"/>
</set>
</class>
</hibernate-mapping> |
Le mapping de message :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<hibernate-mapping>
<class name="data.message.MessageBean" table="Message">
<id name="messageId" type="long" column="MESSAGE_ID" >
<generator class="increment"></generator>
</id>
<many-to-one name="object" class="data.object.objectBean"
column="OBJECT_ID">
</many-to-one>
</class>
</hibernate-mapping> |
Le code de la DAO :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public class objectDaoImpl extends HibernateDaoSupport implements objectDao {
public int getMessagesNumber(Long objectId) throws ObjectNotFoundException {
objectBean object = (objectBean)getHibernateTemplate().get(objectBean.class, objectId) ;
if (object==null) {
logger.error("L'objet d'identifiant "+objectId+" n'a pas été trouvé dans la base.") ;
throw new ObjectNotFoundException("L'objet d'identifiant "+objectId+" n'a pas été trouvé dans la base.") ;
}
Set messages = object.getMessage() ;
return messages.size();
} |
Mon code de test fait, tout simplement, un appel à objectDao.getMessagesNumber(..).
Le test leve une exception :
org.hibernate.LazyInitializationException: illegal access to loading collection
Pourtant j'utilise bien un ProxyFactoryBean qui devrait spécifier qu'une session doit etre ouverte en début de méthode de objectDao puis fermée en fin de méthode.
Autre point bizarre, si je définis le set "messages" en lazy="false", il se passe la meme chose. A la récupération de l'objet.
Je crois avoir fouillé entierement ce forum sur le question. Rien ne peut résoudre mon probleme.
Quelqu'un aurait une idée ?
Merci
Y