-->
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.  [ 4 posts ] 
Author Message
 Post subject: Simple product/category implementation
PostPosted: Mon Nov 08, 2004 6:28 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I'm new to Hibernate and am getting a bit confused about bags, sets, etc. I'm hoping someone can gently point me in the right direction to enable me to implement a simple relationship between categories and items. It mainly works, except for joins.

An item belongs to one and only one category (although I'm wondering whether I should also allow the possibility of uncategorized items). I'm interested in getting a list of items showing their categories, and also selecting a list of items by category. I don't want to automatically load all items when I load a category, but I do want to automatically load the associated Category for an item. It's a simple one-to-many relationship, and if I were thinking in pure SQL terms, I'd have a category_id foreign key in the Items table, linked to the id primary key of the Categories table. But I'm trying to think in object terms instead, and use the automatic generation of tables from Hibernate to produce the schema.

Here's my first pass at the mapping files (extraneous properties not shown):

First, Category.hbm.xml

<?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>
<class name="testing.Category" table="categories" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="integer" unsaved-value="-1">
<generator class="identity"></generator>
</id>
<property name="name" type="string" update="true" insert="true" access="property" column="name" length="40" not-null="true"/>
<bag name="items" lazy="false" inverse="true" cascade="save-update">
<key column="category_id"></key>
<one-to-many class="testing.Item"/>
</bag>
</class>
</hibernate-mapping>

Item.hbm.xml:

<?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>
<class name="testing.Item" table="items" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="integer" unsaved-value="-1">
<generator class="identity"></generator>
</id>
<property name="name" type="string" update="true" insert="true" access="property" column="name" length="80" not-null="true"/>
<many-to-one name="category" class="testing.Category" cascade="none" outer-join="auto" update="true" insert="true" access="property" column="category_id"/>
</class>
</hibernate-mapping>

In my Category class, I have an items member, a List, with getter/setter methods. In my Item class, I have a Category member with getter/setter methods.

In my test method, I can add categories and items OK, and select them OK. However, the following method doesn't work - it always returns 0 items, even with a valid category. (NB This is using Hibernate from within the Spring Framework but the hql should be clear enough anyway):

public List getItemsByCategory(String categoryName) {
String hql="select item from testing.Item as item join item.category as c where c.name=? order by item.name";
return getHibernateTemplate().find(hql,categoryName);
}

Why does this join not work? Or, more simply put, how should I model this simple relationship so I can do precisely what I have outlined above?

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 08, 2004 6:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
just do

Code:
select item from testing.Item as item where item.category.name=? order by item.name


There is no need to explicitly join for to-one relations. Also enable SQL logging and look at the generated SQL if you encounter problems, it often helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 08, 2004 6:36 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
I should have said that this is the SQL generated by the failing method:

Hibernate: select item0_.id as id, item0_.listed_date as listed_d2_, item0_.name as name, item0_.description as descript4_, item0_.picture as picture, item0_.thumbnail_picture as thumbnai6_, item0_.price as price, item0_.sold_date as sold_date, item0_.category_id as category9_ from items item0_ inner join categories category1_ on item0_.category_id=category1_.id where (category1_.name=? ) order by item0_.name


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 08, 2004 6:45 am 
Regular
Regular

Joined: Sun Nov 07, 2004 3:39 pm
Posts: 77
michael wrote:
just do

Code:
select item from testing.Item as item where item.category.name=? order by item.name


There is no need to explicitly join for to-one relations. Also enable SQL logging and look at the generated SQL if you encounter problems, it often helps.


Wonderful! A virtually instantaneous helpful and correct answer! It works now. Is there anywhere other than the reference that this business of to-one relationship and joins is documented, do you know? I'd like to understand things a bit better.

Also, from my description of the requirements, have I got my 'lazy' settings right? I should have mentioned that I do NOT want the deletion of a category to result in the deletion of items in that category (it should ideally go to null).


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