-->
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: HQL Query
PostPosted: Thu Feb 18, 2010 6:48 pm 
Newbie

Joined: Wed Feb 17, 2010 12:46 pm
Posts: 6
I am getting this error:
Quote:
Unable to resolve path [DBO_Contacts.xid], unexpected token [DBO_Contacts] ...

my HBM lookes like:
Code:
    <class name="DBO_ContactInfo" table="ContactInfo">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <one-to-one name="xid" class="DBO_Contacts" fetch="join"/>
        <property name="ContactInfo"/>
        <property name="Private"/>
    </class>

Code:
      Session newSession = HibernateUtil.getSessionFactory().openSession();
      List<DBO_ContactInfo> messages = (List<DBO_ContactInfo>)newSession.createQuery("from DBO_ContactInfo where DBO_Contacts.xid=20").list();
      newSession.close();

This produces that error...how do I fetch a set of (emails, phones) from ContactInfo which is joined to Contacts (Last, First, Middle, Title) based upon something in the Contacts table?

Also if I do this:
Code:
      Session newSession = HibernateUtil.getSessionFactory().openSession();
      List<DBO_ContactInfo> messages = (List<DBO_ContactInfo>)newSession.createQuery().list();
      newSession.close();

It produces multiple queries instead of one
Quote:
select contactinf0_.xid as xid3_, contactinf0_.id as id3_, contactinf0_.id as id25_2_, contactinf0_.ContactInfo as ContactI2_25_2_, contactinf0_.Private as Private25_2_, dbo_contac1_.id as id24_0_, dbo_contac1_.xid as xid24_0_, dbo_contac1_.Last as Last24_0_, dbo_contac1_.First as First24_0_, dbo_contac1_.Middle as Middle24_0_, dbo_contac1_.Title as Title24_0_, dbo_tconta2_.id as id11_1_, dbo_tconta2_.ContactType as ContactT2_11_1_ from ContactInfo contactinf0_ left outer join Contacts dbo_contac1_ on contactinf0_.id=dbo_contac1_.id left outer join tContactType dbo_tconta2_ on dbo_contac1_.id=dbo_tconta2_.id where contactinf0_.xid=? order by contactinf0_.Type asc


with the problem being the added where contactInf0_.xid=?
instead of just this:
Code:
mysql> select * from Contacts;
+----+------+-------+-------+--------+-------+------+
| id | xid  | Last  | First | Middle | Title | Type |
+----+------+-------+-------+--------+-------+------+
|  1 |   20 | Foo   | Bar   | W      | NULL  |    1 |
|  2 |   22 | Smith | John  | NULL   | NULL  | NULL |
+----+------+-------+-------+--------+-------+------+

mysql> select * from ContactInfo;
+----+------+----------------+------+---------+
| id | xid  | ContactInfo    | Type | Private |
+----+------+----------------+------+---------+
|  1 |    1 | foo@bar.org    |    8 |    NULL |
|  2 |    2 | john@smith.com |    8 |    NULL |
| 11 |    1 | 111-222-3333   |    2 |    NULL |
+----+------+----------------+------+---------+

mysql> select * from ContactInfo, Contacts where ContactInfo.xid = Contacts.id;
+----+------+----------------+------+---------+----+------+-------+-------+--------+-------+------+
| id | xid  | ContactInfo    | Type | Private | id | xid  | Last  | First | Middle | Title | Type |
+----+------+----------------+------+---------+----+------+-------+-------+--------+-------+------+
|  1 |    1 | foo@bar.org    |    8 |    NULL |  1 |   20 | Foo   | Bar   | W      | NULL  |    1 |
| 11 |    1 | 111-222-3333   |    2 |    NULL |  1 |   20 | Foo   | Bar   | W      | NULL  |    1 |
|  2 |    2 | john@smith.com |    8 |    NULL |  2 |   22 | Smith | John  | NULL   | NULL  | NULL |
+----+------+----------------+------+---------+----+------+-------+-------+--------+-------+------+

The 3rd one being the desired join.


Top
 Profile  
 
 Post subject: Re: HQL Query
PostPosted: Fri Feb 19, 2010 4:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to assign an alias to the root entity and use the name of the association (not the class name). Try something like:

Code:
from DBO_ContactInfo ci where ci.xid.id=20


Top
 Profile  
 
 Post subject: Re: HQL Query
PostPosted: Fri Feb 19, 2010 11:54 am 
Newbie

Joined: Wed Feb 17, 2010 12:46 pm
Posts: 6
That gets me closer, but doesn't create the right sql
Code:
from DBO_ContactInfo ci where ci.xid.id=20

<one-to-one name="xid" class="DBO_Contacts" fetch="join"/>

select dbo_contac0_.id as id25_, dbo_contac0_.ContactInfo as ContactI2_25_, dbo_contac0_.Private as Private25_ from ContactInfo dbo_contac0_ where dbo_contac0_.id=20

However as seen in the sql, the where is not on "where Contacts.id = 20" so how do I reference Contacts?

Peter


Top
 Profile  
 
 Post subject: Re: HQL Query
PostPosted: Fri Feb 19, 2010 12:36 pm 
Newbie

Joined: Wed Feb 17, 2010 12:46 pm
Posts: 6
duh!!!

from DBO_ContactInfo ci, DBO_Contacts c where c.xid=20

Thanks for the pointer in the right direction!

Peter


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.