Hello,
I started learning Hibernate (join with spring) for few days and I have a very simple question. I'm doing a unit test to be sure my different layers works well and I wonder about a collection loading problem. Here is a part of my spring+hibernate conf :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.nomia.onmap.model.Value</value>
<value>com.nomia.onmap.model.Attribut</value>
<value>com.nomia.onmap.model.BooleanValue</value>
<value>com.nomia.onmap.model.Element</value>
<value>com.nomia.onmap.model.LocString</value>
<value>com.nomia.onmap.model.Map</value>
<value>com.nomia.onmap.model.Picto</value>
<value>com.nomia.onmap.model.Relation</value>
<value>com.nomia.onmap.model.LocStringValue</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="
com.nomia.onmap.controller,
com.nomia.onmap.service,
com.nomia.onmap.dao"/>
</beans>
My unit test is like that :
Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:unittest-spring-all.xml")
@TransactionConfiguration(defaultRollback=true,transactionManager="transactionManager")
public class ElementHibernateDaoImplTest {
private final static Log _log = LogFactory.getLog(ElementHibernateDaoImplTest.class);
@Autowired
private ElementService elementService;
@Test
public void testCreateElement() {
Attribut att = new Attribut();
...
Element element = new Element();
element.getAttributs().add(att);
elementService.save(element);
assertNotNull(element.getId());
_log.info("Number of attributs : "+element.getAttributs());
for (Attribut attribut : element.getAttributs()) {
_log.info(attribut);
}
//Until here, everything works very well, no problem. Datas are registerd in database.
Element elt = elementService.getElement(element.getId(), true);
_log.info("Number of attributs : "+elt.getAttributs());//Here, I have an exception depending on what I write in my DAO (see just after please)
for (Attribut attribut : elt.getAttributs()) {
_log.info(attribut);
}
As I explain in this code, I have sometimes an exception (org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.nomia.onmap.model.Element.attributs, could not initialize proxy - no Session) which I do not understand :-) Let me explain :
Here is my dao:
Code:
@Transactional(readOnly = true)
public Element getElement(long elementId, boolean loadAttributs){
Element element = (Element)sessionFactory.getCurrentSession().get(Element.class, elementId);
if(loadAttributs){
Set<Attribut> attributs = element.getAttributs();
//_log.debug(attributs.size()+" attributs loadded");
}
return element;
}
If I uncomment the _log.debug(attributs.size()+" attributs loadded"); line everything works well. But if I comment it (like now) i have the exception as I say in my unit test. However I thought that element.getAttributs() should be enought to load my collection as my session is open in this method.
So is there a better way to load my lazy collection than adding a log... What do I do wrong please ?
Thank's in advance !
Clement