-->
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.  [ 2 posts ] 
Author Message
 Post subject: help with a simple hibernate query - i am a sql dummy.
PostPosted: Thu Oct 09, 2003 3:58 pm 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
I have persistable java classes "eg.Story" and "eg.Author"

Author is a property of Story, as is LanguageCode

I wish to find all Stories by that Author

right now I am trying somthing along the lines of

Code:
List stories = sess.find("from story in class eg.Story where story.author = ? and story.languageCode = ?", new Object[]{someauthor,somelangcode}, new Type[]{eg.Author, Hibernate.STRING});


but obviously that fails as the compiler wants me to provide a hibernate Type, not a class as I first guessed.

I have googled about for some examples but alas have come up dry. I keep seeing references to 'left joins' and suspect that may be what I am after, but like I say in my subject line, I am a sql dummy and have no idea what a 'left join' is.

can someone help me with this, or point me at some good docs where i can read up on HQL for SQL dummies?

also if I don't care about the language code for some queries then is is acceptable to set
Code:
String somelanguagecode = "*";

and assume that it will function as a wildcard, or do i have to test for null and change my query accordingly?

dave


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2003 4:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Use the Hibernate.entity() method...

Code:
List stories = sess.find("from eg.Story as story where story.author = ? and story.languageCode = ?", new Object[]{someauthor,somelangcode}, new Type[]{Hibernate.entity(eg.Author), Hibernate.STRING});


Or even:
Code:
List stories = sess.find("from eg.Story as story where story.author.id = ? and story.languageCode = ?", new Object[]{someauthor.getId(),somelangcode}, new Type[]{Hibernate.LONG, Hibernate.STRING});


I manually check for nulls and build up appropriate HQL. The other option is to use your database specific wildcard character (on Oracle, for example, you'd have to change the = to LIKE and use % as the wildcard).

A left join is a type of "optional" or "outer" join. Thats probably not what you want here. Optional joins say to return rows in the joined result even if there is no match in the joined table. Here it would basically say to return a Story even if there was no Author associated. Outer joins are typically slower.

HTH


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