-->
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.  [ 3 posts ] 
Author Message
 Post subject: BeanCreationException - no session or session was closed
PostPosted: Thu May 31, 2007 2:43 pm 
Newbie

Joined: Thu May 31, 2007 2:04 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 2:53 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
set lazy="false" on your kategories collection to get it to not error. Then go read about lazy initialization to find out how you actually want to do that.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 2:56 pm 
Newbie

Joined: Thu May 31, 2007 2:04 pm
Posts: 2
i thought it didn't work but it seems like i made a mistake... tryed it again and it works... thanks man!

hmmmm...

i'm sorry for this thread....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.