-->
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: Get one-to-many Related Object List Using Criteria
PostPosted: Mon May 18, 2009 6:55 am 
Newbie

Joined: Wed May 06, 2009 6:10 am
Posts: 4
Hi.

I have two objects which one of them is Person.class, and another one is PhoneNumber.class.
There is a relationship between Person and PhoneNumber and a person may have more than one phonenumber. Their relationship is linked with this;

Code:
   <class name="Person" table="persons">
      <id name="id" column="ID">
         <generator class="native">
            <param name="sequence">SEQ_PERSON</param>
         </generator>
      </id>
      <bag name="phoneNumbers" cascade="all" lazy="true">
         <key not-null="true">
            <column name="PERSON_ID" />
         </key>
         <one-to-many class="PhoneNumber" />
      </bag>
           </class>


Now, i want to get PhoneNumber list from Person using Criteria. But i couldn't find a solution about this. The important thing is that i want to get List of PhoneNumber, not a Person.

And this is the Person.class

Code:
public class Person {

   private Long id;

   private Collection phoneNumbers;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id= id;
   }

   public Collection getPhoneNumbers() {
      return phoneNumbers;
   }

   public void setPhoneNumbers(Collection phoneNumbers) {
      this.phoneNumbers= phoneNumbers;
   }

}


How can i get this?

Thanks for any help.


Top
 Profile  
 
 Post subject: Re: Get one-to-many Related Object List Using Criteria
PostPosted: Mon May 18, 2009 7:23 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
Hi,
I dont know, whether i ve got your question right?
But i m trying to answer your query based on some assumptions.

try the following code.

Criteria criteria = getSession().createCriteria( Person.class ); // i ve extended the HibernateDaoSupport class.
criteria.add(Restrictions.eq("id",1234));
criteria.add(Restrictions.between("hiredate", '05/05/2004', '05/10/2004');
List persons = criteria.list();

now you will get all the persons matching the above criteria in the persons list.

Person person = (Persons) persons.get(0);
System.out.println(persons.getPhoneNumbers()); //override the toString method in PhoneNumbers class to display the phone numbers.

i hope it helps.

if not, i ve not got your question correctly.


Top
 Profile  
 
 Post subject: Re: Get one-to-many Related Object List Using Criteria
PostPosted: Mon May 18, 2009 7:30 am 
Newbie

Joined: Wed May 06, 2009 6:10 am
Posts: 4
Hi. Your solution is true but there is a problem about it.

This criteria gets person and i get phonenumbers over it. When this method ends i have a phonenumbers list, but the person object still wait on the session and if i want to delete phonenumbers from where i call it like bean i get an error. Because of this i want to get phonenumbers list directly from criteria. I may get a list of property with type string, long, date etc but i cant get collections over criteria.


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.