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="false"
>
<id
name="id"
column="CATEGORY_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="increment">
<!--
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="false"
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 findCategoryByName(Category name){
Collection categories = getHibernateTemplate().find("from Category cat where cat.name = ?",
new Object[]{name.getName()});
return categories;
}
public void testFindCategory() throws Exception {
Category cat = new Category("Vehicle");
Collection allCategory = dao.findCategoryByName(cat);
for (Iterator i = allCategory.iterator(); i.hasNext(); ) {
cat = (Category)i.next();
log.info("vehicle = " + cat.getId() + " " + cat.getName());
Category parent = cat.getParentCategory();
if(parent != null)
log.info("parent = " + parent.getId() + " " + parent.getName());
for (Iterator j = cat.getChildCategories().iterator(); j.hasNext(); ) {
cat = (Category)j.next();
log.info("vehicleChild = " + cat.getId() + " " + cat.getName());
}
}
}
Name and version of the database you are using: MySql 4.1
hi guys,
I was testing category using composite pattern and encounter something that i don't really understand. when i run the above test code with the above mapping, I got output like:
[junit] 15:48:22,357 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 15:48:22,357 INFO CatalogDAOTest:61 - vehicleChild = 2 Car
[junit] 15:48:22,357 INFO CatalogDAOTest:61 - vehicleChild = 5 Truck
when i changed all
lazy="true" and the query to
Collection categories = getHibernateTemplate().find("from Category cat inner join fetch cat.childCategories right join fetch cat.parentCategory where cat.name = ?",new Object[]{name.getName()});
i got output like:
[junit] 16:31:24,940 INFO ConnectionManager:282 - Aggresively releas
[junit] 16:31:24,940 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
[junit] 16:31:24,950 INFO CatalogDAOTest:53 - vehicle = 1 Vehicle
my category data is like below:
Vehicle
- Car
- Small Car
- Big Car
- Truck
- Small Truck
- Big Truck
Appreciate if someone can help, thanks in adv !