-->
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: Shorter HQL Query avalable
PostPosted: Mon Sep 12, 2005 10:33 am 
Newbie

Joined: Mon Sep 12, 2005 10:13 am
Posts: 2
Location: switzerland
is there a shorter hql query available for this function?

I thought of something like "from Book where authors.name like ?" instead of "from Author where name like ?" where i need to loop through the authors.


Function:

Code:
public List getBooksByAuthor(String author) throws DataAccessException
{
   List authorList= getHibernateTemplate().find("from Authors where name like ?", author);
   List bookList= new java.util.ArrayList();
   for (int i=0;i<authorList.size();i++)
   {
      Author myAuthor= (Author)authorList.get(i);
      bookList.add(myAuthor.getBook());         
   }
   return bookList;
}



Hibernate Mapping:

Code:
<class name="Book" table="book">
   <id name="id" column="id" unsaved-value="0">
      <generator class="increment" />
   </id>
   <property name="name" column="name" not-null="true" />
   <set name="authors" cascade="all" inverse="false" lazy="false">
           <key column="book_id"/>
           <one-to-many class="Author"/>
      </set>
</class>
   
<class name="Author" table="author">
   <id name="id" column="id" unsaved-value="0">
      <generator class="increment" />
   </id>
   <property name="name" column="name" not-null="true" />
   <many-to-one name="book" class="Book" column="book_id" insert="false"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 10:50 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Hi,

I would expect something like:

Quote:
List bookList= getHibernateTemplate().find("SELECT b from BOOK b, b.authors as a where a.name like ?", author);

to be what you want.

Note: I wrote this without testing it, so there will probably be some syntax error, but the intention is correct: use the association between book and author to access all the authors associated to the book and verify that their names are as you expect, an return all the matching books.

Hope that helps

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 11:05 am 
Newbie

Joined: Mon Sep 12, 2005 10:13 am
Posts: 2
Location: switzerland
Hi Erik,

Thanx for your help. I tried a little and solved it. I came up with:

Code:
return getHibernateTemplate().find("from Book as b where b.authors.name like ?",author);


which works fine now.


ErikFK wrote:
Hi,

I would expect something like:

Quote:
List bookList= getHibernateTemplate().find("SELECT b from BOOK b, b.authors as a where a.name like ?", author);

to be what you want.

Note: I wrote this without testing it, so there will probably be some syntax error, but the intention is correct: use the association between book and author to access all the authors associated to the book and verify that their names are as you expect, an return all the matching books.

Hope that helps

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 11:08 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Quote:
return getHibernateTemplate().find("from Book as b where b.authors.name like ?",author);

Yep, that's nicer than what I proposed - and should generate the same SQL statement.

Have a nice time with Hibernate :-)

Erik


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.