I'm using Hibernate 2.1.1.
I'm using "fetch" in the query to initialize the collection!
I don't understand why is he giving me the following error:
Code:
ERROR: Failed to lazily initialize a collection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:213)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:69)
at net.sf.hibernate.collection.Set.toString(Set.java:219)
( ... )
-----------------------------------------------------
Mapping file (generated with Middlegen R3)
-----------------------------------------------------
MisturaCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="vo.Mistura"
table="mistura"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to MisturaStock -->
<set
name="misturaStocks"
lazy="true"
inverse="true"
>
<key>
<column name="mistura_fk" />
</key>
<one-to-many
class="vo.MisturaStock"
/>
</set>
</class>
</hibernate-mapping>
-----------------
Testing code
-----------------
Code:
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
res = session.find("from vo.Mistura as m " +
"left join fetch misturaStocks " +
"where m.userId= '" + user + "'");
session.close();