针对多对多和多个标准的CF9 HQL语句

时间:2010-03-26 03:40:11

标签: hibernate orm coldfusion hql coldfusion-9

我有以下设置:

Listing.cfc

component persistent="true"
{
    property name="ListingId" column="ListingId" type="numeric" ormtype="int" fieldtype="id" generator="identity"; 
        ...
    property name="Features" type="array" hint="Array of features" singularname="Feature" fieldtype="many-to-many" cfc="Feature" linktable="Listing_Feature" FKColumn="ListingId" inversejoincolumn="FeatureId";    
} 

Feature.cfc

component persistent="true" table="Feature" schema="dbo" output="false"
{
    property name="FeatureId" column="FeatureId" type="numeric" ormtype="int" fieldtype="id" generator="identity"; 
    property name="FeatureDescription" column="FeatureDescription" type="string" ormtype="string";
    ...    
    /*property name="Listings" fieldtype="many-to-many" cfc="Listing" linktable="Listing_Feature" fkcolumn="FeatureId" inversejoincolumn="ListingId" lazy="true" cascade="all" orderby="GroupOrder";*/

} 

我可以使用以下选项来选择具有特定功能的所有商家信息:

<cfset matchingListings = ormExecuteQuery("from Listing l left join l.Features as feature where feature.FeatureId = :feature",{feature = 110}) />

但是,我希望能够选择所有具有多种功能的商品(例如同时包含“洗碗机”和“车库”的商品详情)

经过几个小时的谷歌搜索并查看hibernate文档后,找不到一个不会给我错误的解决方案。我的猜测是解决方案非常简单,我只是过度思考......任何人都有任何建议吗?

2 个答案:

答案 0 :(得分:4)

我不相信这是最有效的方法,但是,它确实产生了我想要的结果

<cfset matchingListings = ormExecuteQuery("Select l.ListingId from Listing l
  left join l.Features as featureone left join l.Features as featuretwo
  left join l.Features as featurethree 
  where featureone.FeatureId = 108
    and featuretwo.FeatureId = 110
    and featurethree.FeatureId = 113") />

这只会给我一些具有我正在寻找的所有功能的列表但是,它有很多加入并查看hibernate SQL日志正在生成:

select listing0_.ListingId as col_0_0_ 
from dbo.Listing listing0_ 
left outer join Listing_Feature features1_ on listing0_.ListingId=features1_.ListingId 
left outer join dbo.Feature feature2_ on features1_.FeatureId=feature2_.FeatureId
left outer join Listing_Feature features3_ on listing0_.ListingId=features3_.ListingId 
left outer join dbo.Feature feature4_ on features3_.FeatureId=feature4_.FeatureId 
left outer join Listing_Feature features5_ on listing0_.ListingId=features5_.ListingId 
left outer join dbo.Feature feature6_ on features5_.FeatureId=feature6_.FeatureId 
where 1=1 
and feature2_.FeatureId=108 
and feature4_.FeatureId=110 
and feature6_.FeatureId=113

似乎必须有一种更有效的方法在HQL中执行此操作


在cf-orm-dev邮件列表上的Jon Messer给了我一些我认为最合适的解决方案,这个问题在这里发布给大家:

“据我所知,ORMExecuteQuery不处理列表参数,所以如果你想要它们并且返回对象,你必须做类似的事情

<cfset featureIds = [javaCast('int',108), javaCast('int',110), javaCast('int',113)] >

<cfset q = ormGetSession().createQuery("
    select l.ListingId 
    from Listing l 
        join l.features as f 
    where f.FeatureId in (:features)
    group by l.ListingId
    having count(*) = #arrayLen(featureIds)#
") /> 

<cfset q.setParameterList('features', featureIds) /> 

<cfset matchingListings = q.list() />

谢谢Jon!

答案 1 :(得分:0)

这应该有效:

<cfset featurelist = "110,113,125"/>
<cfset matchingListings = ormExecuteQuery("from Listing l left join l.Features as feature where feature.FeatureId IN (#featurelist#)")/>

如果你想使用绑定参数,你将不得不做一些额外的工作,因为这次hibernate不喜欢ColdFusion Lists和/或Arrays作为绑定参数。您可以找到一些额外信息here