-->
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.  [ 2 posts ] 
Author Message
 Post subject: Incorrect SQL generated
PostPosted: Mon Oct 24, 2005 4:46 pm 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
I have a formula imbedded in my hbm file as follows:

Code:
    <class
        name="HighEnergy.Domain.ContactImpl, HighEnergyCore"
        table="CONTACT"
        dynamic-insert="false"
        dynamic-update="false"
        >
    ...
        <property name="CountSelfCompanies" type="Int64" formula="(select count(*) from company_contact cc, company c where cc.company_fk=c.id and cc.role=0 and c.type=0 and cc.contact_fk=id)" />
    </class>


NHibernate seems to generate incorrect SQL when it tries to fetch this entity:
Code:
SELECT
    ...
    (select
         count(*)
     from
         company_contact cc,
         contacti1_.company contacti1_.c  <---- This is the line where I get a syntax error
     where
         cc.company_fk=c.id and
         cc.role=0 and
         c.type=0 and
         cc.contact_fk=contacti1_.id) as f1_0_
FROM
    TRADE_CONTACT contacts0_
    left outer join CONTACT contacti1_ on contacts0_.CONTACT_FK=contacti1_.ID
WHERE
    contacts0_.TRADE_FK = 201
ORDER BY contacts0_.TRADE_FK


This is the error I am getting:
Code:
Incorrect syntax near '.'



Why does NHibernate change "company c" to "contacti1_.company contacti1_.c"? Am I doing something wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 12:24 am 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
Okay I solved the problem. Turns out that NHibernate generates incorrect syntax if there are commas in the query (this is based on http://nhibernate.sourceforge.net/forum/viewtopic.php?t=1068&highlight=formula). So I changed the query to have a subselect and it worked:
Code:
(select count(*) from company c
where c.id in (select cc.company_fk from company_contact cc where cc.contact_fk=id and cc.role=0)
   and c.type=0)


Note that the query did not work even if I used the new SQL syntax with "inner join" to remove the comma (this was suggested by another person in the same thread mentioned above).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.