-->
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.  [ 5 posts ] 
Author Message
 Post subject: Need hrlp on lazy="true" and inner join fetch
PostPosted: Tue Jun 14, 2005 4:40 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
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 !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 5:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Not looking in detail. First impression, I don't think fetch is implemented on anything other than for outer join.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 5:51 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
david wrote:
Not looking in detail. First impression, I don't think fetch is implemented on anything other than for outer join.


I got this query from hibernate site:

from eg.Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens


http://www.hibernate.org/hib_docs/refer ... ryhql.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 10:57 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
actually i just need to know how to write Hibernate SQL query in order to get the same result when I set all lazy="true" ! I know that if lazy="false", i don't need to specifically do a 'fetch' in the hibernate sql statement ! pls help, Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 8:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Looking briefly;


select cat from Category as cat left join fetch cat.parentCategory as pcat left join fetch cat.childCategories as ccat where cat.name = ?


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