-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query into collection
PostPosted: Mon Mar 03, 2008 5:20 pm 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:17 pm
Posts: 44
I'm having trouble figuring out how to come up with a query (Criteria or HQL) for this, but it seems like it shouldn't be too hard.

Here's what I have:

Category is an Enum.

class Content:
Collection categories


In my config, I'm mapping categories like this:

Code:
        <bag name="categories" table="contents_categories" cascade="all" lazy="true">
            <key column="contentId" />
            <element column="category" type="com.foo.hibernate.usertypes.CategoryUserType"/>
        </bag>


CategoryUserType extends the EnumUserType listed here: http://www.hibernate.org/265.html


The relevant tables and fields look like this:

Code:
DROP TABLE IF EXISTS `foo`.`content`;
CREATE TABLE  `foo`.`content` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `foo`.`contents_categories`;
CREATE TABLE  `foo`.`contents_categories` (
  `contentId` bigint(20) unsigned NOT NULL auto_increment,
  `category` varchar(45) NOT NULL default '',
  PRIMARY KEY  (`contentId`,`category`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



What I'd like to be able to query is getting back all of the Content objects that have a given Category in it's list of categories. Doesn't seem like it should be too hard, but I'm not sure how to get back Content based on the categories that it has.

Any help is greatly appreciated. We're using Criteria for our other queries, but either Criteria or HQL is fine.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Query into collection
PostPosted: Mon Mar 03, 2008 5:35 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am not sure about the custom type part but this might work:

Code:
select c from Content c inner join c.categories cat where cat = :category




Farzad-


Top
 Profile  
 
 Post subject: Re: Query into collection
PostPosted: Tue Mar 04, 2008 10:09 am 
Beginner
Beginner

Joined: Thu Oct 23, 2003 1:17 pm
Posts: 44
farzad wrote:
I am not sure about the custom type part but this might work:

Code:
select c from Content c inner join c.categories cat where cat = :category


Farzad-


That worked perfectly, and handled the EnumUserType nicely.

Thanks Farzad.


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