-->
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: A problem of many-to-one query
PostPosted: Wed May 14, 2008 12:27 am 
Newbie

Joined: Wed Nov 22, 2006 7:56 am
Posts: 2
I have two tables called "Homework" and "Course". There is a property "course_id" in "Homework" as the foreign key joining the table "Course". And I use Hibernate Synchronizer to general the mapping files and DAO classes. The problem is: I want to get all homework of a course by course_id. The mapping script of the column "course_id" is as bellowed:

Code:
      <many-to-one
         name="Course"
         column="course_id"
         class="Course"
         not-null="true"
         outer-join="true"
         lazy="false"
      >


How to make this query? Thanks very much!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 1:00 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
HQL:
------

from HomeWork hw where hw.course=?


Don't use caps for starting letter of property like Course.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 10:06 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I think the most object oriented way is to use the Criteria API. It might look something like this. As you can see, no HQL - just Java objects:



Code:
    Homework homework = new Homework();
    h.setCourse(new Course("Java101");
    Example example = Example.create(homework);
    Session session = HibernateUtil.beginTransaction();
    Criteria criteria = session.createCriteria(Homework.class);
    criteria.add(example);
    List results = criteria.list();
    HibernateUtil.commitTransaction();
    for (int i = 0; i<results.size(); i++) {
      System.out.println(results.get(i).toString());
    }


Here's a free tutorial on the Criteria API if you're interested:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.