Hey,
i just trying to build a little hibernate test... it works nearly fine but:
Code:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'katTest' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: kat.beans.Kategorie.kategories, no session or session was closed
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: kat.beans.Kategorie.kategories, 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.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:114)
at kat.KatTest.init(KatTest.java:21)
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:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1241)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1206)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:428)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
at kat.Launcher.main(Launcher.java:9)
The Class "Launcher" just loads the context and my test Class "KatTest" looks like this:
Code:
Kategorie root = kategorieDAO.findById(1);
System.out.println(root.getName()); // Works fine...
Collection<Kategorie> childs = root.getKategories(); // Throws the BeanCreationException
System.out.println("Size: " + childs.size());
Iterator<Kategorie> it = unterKategorien.iterator();
while (it.hasNext()) {
Kategorie current = it.next();
System.out.println(" - " + current.getName());
}
My Application-Context...
Code:
<bean id="hibernateDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://server/db">
</property>
<property name="username" value="user"></property>
<property name="password" value="pass"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="hibernateDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>kat/beans/Kategorie.hbm.xml</value>
</list>
</property>
</bean>
<bean id="kategorieDAO" class="kat.beans.KategorieDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="katTest" class="kat.KatTest" init-method="init">
<property name="kategorieDAO" ref="kategorieDAO" />
</bean>
My Kategorie.hbm.xml
Code:
<hibernate-mapping>
<class name="kat.beans.Kategorie" table="kategorie" catalog="db">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment"></generator>
</id>
<many-to-one name="kategorie" class="kat.beans.Kategorie" fetch="select">
<column name="parentId" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<set name="kategories" inverse="true">
<key>
<column name="parentId" />
</key>
<one-to-many class="kat.beans.Kategorie"/>
</set>
</class>
</hibernate-mapping>
Whats to do that it doesn't lose it's session?
I'm nearly sure this question was asked before but i didn't find it...
Thx for your help...
Greets