- This topic has 1 reply, 1 voice, and was last updated 19 years, 1 month ago by
SEanLon11.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
SEanLon11MemberI am trying to run a query on my “contacts” to “contact_phones” to “phones” tables. I have read through other forums, and even looked it up in my handy “Hibernate in Action” book but am still lost. Any help would be appreciated.
The Contact_Phone class mapping file:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Phones.Contact_Phone" table="contacts_phones" schema="public"> <composite-id name="id" class="Phones.Contact_PhoneId"> <key-many-to-one name="phones" class="Phones.Phone"> <column name="phoneid" /> </key-many-to-one> <key-many-to-one name="contacts" class="Contacts.Contact"> <column name="contactid" /> </key-many-to-one> </composite-id> </class> </hibernate-mapping>
Where I am calling it from:
/** * */ package Libraries.Getters; import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Restrictions; import Contacts.Contact; import Libraries.Session.SessionHelper; import Phones.Contact_Phone; import Phones.Phone; /** * @author Owner * */ public class GetContactInfo { SessionHelper session = new SessionHelper(); /** * Sets the passed in Contact's Set of Phones. * @param c */ public void populatePhones(Contact c) { // Contact x = new Contact("M", "Bob", "last", new Date()); // Clearing the set in order to have the // most up to date phone numbers c.getContactsPhoneses().clear(); String SQL_QUERY = "from Contact c join fetch c.contactsPhoneses " + "where c.contactid = 33"; String sql = "from Contact_Phone p join fetch p.id"; // " + //"where c.contactid = 33"; //"Contact_Phone cp "; // "c.contactid = 33"; Query query = session.getSession().createQuery(sql); /*Criteria crit = session.getSession().createCriteria(Contact_Phone.class); crit.add(Restrictions.like("id", c.getContactid())); //Like condition //crit.add(Restrictions.between("id", 12, 20)); // crit.setMaxResults(6); // Restricts the max rows List mess = (List) crit.list(); */ List mess = (List) query.list(); //List mess = getCriteria(); for (Iterator it = mess.iterator(); it.hasNext();) { Contact phone = (Contact) it.next(); //extracting each instance // c.getContactsPhoneses().add(phone); // adding the Phone instance to the Phone Set of c System.out.println("id = " + phone.getContactid()); } //System.out.println("-- Iteration Successful -- "); } }
I will keep looking on my own, but any suggestions would be appreciated.
Thanks,
Sean
SEanLon11MemberThese also might be helpful:
The AbstractContact_Phone class:
package Phones; /** * AbstractContact_Phone generated by MyEclipse - Hibernate Tools */ public abstract class AbstractContact_Phone implements java.io.Serializable { // Fields private Contact_PhoneId id; // Constructors /** default constructor */ public AbstractContact_Phone() { } /** full constructor */ public AbstractContact_Phone(Contact_PhoneId id) { this.id = id; } // Property accessors public Contact_PhoneId getId() { return this.id; } public void setId(Contact_PhoneId id) { this.id = id; } }
And the Contact_PhoneID.java
package Phones; import Contacts.Contact; /** * Contact_PhoneId generated by MyEclipse - Hibernate Tools */ public class Contact_PhoneId implements java.io.Serializable { // Fields private Phone phones; private Contact contacts; // Constructors /** default constructor */ public Contact_PhoneId() { } // Property accessors public Phone getPhones() { return this.phones; } public void setPhones(Phone phones) { this.phones = phones; } public Contact getContacts() { return this.contacts; } public void setContacts(Contact contacts) { this.contacts = contacts; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof Contact_PhoneId)) return false; Contact_PhoneId castOther = (Contact_PhoneId) other; return ((this.getPhones() == castOther.getPhones()) || (this .getPhones() != null && castOther.getPhones() != null && this.getPhones().equals( castOther.getPhones()))) && ((this.getContacts() == castOther.getContacts()) || (this .getContacts() != null && castOther.getContacts() != null && this .getContacts().equals(castOther.getContacts()))); } public int hashCode() { int result = 17; result = 37 * result + (getPhones() == null ? 0 : this.getPhones().hashCode()); result = 37 * result + (getContacts() == null ? 0 : this.getContacts().hashCode()); return result; } }
Thanks again,
Sean -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)