Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0.4
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.stufftolet.model.Posting.Category"
table="CATEGORY"
lazy="true"
>
<id
name="id"
column="CATEGORY_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Category.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<version
name="version"
column="version"
type="java.lang.Integer"
/>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
>
<column
name="NAME"
length="50"
unique-key="UNIQUE_NAME_AT_LEVEL"
not-null="true"
/>
</property>
<many-to-one
name="parentCategory"
class="com.stufftolet.model.Posting.Category"
cascade="none"
outer-join="false"
update="true"
insert="true"
foreign-key="FK1_PARENT_CATEGORY_ID"
>
<column
name="PARENT_CATEGORY_ID"
unique-key="UNIQUE_NAME_AT_LEVEL"
not-null="false"
/>
</many-to-one>
<set
name="childCategories"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
batch-size="10"
>
<key
>
<column
name="PARENT_CATEGORY_ID"
/>
</key>
<one-to-many
class="com.stufftolet.model.Posting.Category"
/>
</set>
<set
name="categorizedItems"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
outer-join="false"
>
<key
>
<column
name="CATEGORY_ID"
length="16"
not-null="true"
/>
</key>
<one-to-many
class="com.stufftolet.model.Posting.CategorizedItem"
/>
</set>
<property
name="created"
type="java.util.Date"
update="false"
insert="true"
column="CREATED"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Category.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public Collection findAllCategory(boolean onlyRootCategories){
Collection categories;
if (onlyRootCategories) {
Criteria crit = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Category.class);
categories = crit.add(Expression.isNull("parentCategory")).list();
} else
categories = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Category.class).list();
return categories;
}
Full stack trace of any exception that occurs:
[junit] Testcase: testFindCategory(com.stufftolet.dao.CatalogDAOTest):
Caused an ERROR
[junit] No Hibernate Session bound to thread, and configuration does not all
ow creation of non-transactional one here
[junit] org.hibernate.HibernateException: No Hibernate Session bound to thre
ad, and configuration does not allow creation of non-transactional one here
[junit] at org.springframework.orm.hibernate3.LocalSessionFactoryBean$Tr
ansactionAwareInvocationHandler.invoke(LocalSessionFactoryBean.java:960)
[junit] at $Proxy0.getCurrentSession(Unknown Source)
[junit] at com.stufftolet.dao.hibernate.CatalogDAOHibernate.findAllCateg
ory(CatalogDAOHibernate.java:66)
[junit] at com.stufftolet.dao.CatalogDAOTest.testFindCategory(CatalogDAO
Test.java:51)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcces
sorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet
hodAccessorImpl.java:25)
Name and version of the database you are using:MySql 4.1.12
Hi guys,
When I was testing a composite pattern retrieve function, which is
categories = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Category.class).list();
i got the above exception. Anyone pls kindly assist, Thank you !
regards,
prettyhandling