-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate criteria query puzzler of the day
PostPosted: Sun Feb 13, 2005 8:24 pm 
Newbie

Joined: Sun Feb 13, 2005 7:58 pm
Posts: 1
Hello, much appreciated to anyone that can help me with this query.

I have a relationship that looks something like this:

City(1)----->(*) City_Category_Relationship (*) <---------(1) Category

Logically, a city falls into many categories, and a category may be assigned to many cities. Or, there is a many-to-many relationship between city and category.

I am mapping this using what I call a 'double many to one', i.e. the 'link entity' is created by me, not hibernate, so that I can add properties to it other than just IDs (recommended at the end of the reference guide).

What I would like to do is come up with a query using the criteria API, that returns me a distinct list of all cites that fall into multiple categories. For example, show me the unique (distinct) list of cities that are 'Large', 'On the East Coast', and 'Cold in the Winter'.

Hibernate version : 2.1.7c

My mapping files look like this:

City
------------------------------------------
<hibernate-mapping>
<class
name="model.City"
table="City"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version">

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null" >
<generator class="native">
</generator>
</id>

<set
name="CityCategoryRelationship"
lazy="false"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
order-by="display_order">

<key
column="city_id">
</key>

<one-to-many
class="model.CityCategoryRelationship"/>
</set>
...


CityCategoryRelationship
----------------------------------------------
<hibernate-mapping>
<class
name="model.CityCategoryRelationship"
table="city_category_relationship"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version">

<id name="id"
column="id"
type="java.lang.Long"
unsaved-value="null" >
<generator class="native">
</generator>
</id>

<many-to-one
name="cityRef"
class="model.City"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="city_id"
/>

<many-to-one
name="categoryRef"
class="model.Category"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="category_id"
/>
...


Category
-------------------------------

<class
name="model.Category"
table="category"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version">

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null" >
<generator class="native">
</generator>
</id>

<set
name="CityCategoryRelationship"
lazy="false"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
order-by="display_order">

<key
column="category_id">
</key>

<one-to-many
class="model.CityCategoryRelationship"/>
</set>


<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
/>
...


Thanks!

CP


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.