用于多对多和多个条件的 CF9 HQL 语句

发布于 2024-08-26 06:18:08 字数 1330 浏览 3 评论 0原文

我有以下设置:

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}) />

这很好,但是,我希望能够选择具有多个功能的所有列表(例如列表既有“洗碗机”又有“车库”)

经过几个小时的谷歌搜索和查看休眠文档后未能找到不会给我错误的解决方案。我的猜测是,解决方案非常简单,我只是想太多了......有人有什么建议吗?

I have the following setup:

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";*/

} 

I can select all listings that have a particular feature using:

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

Which is fine, however, I'd like to be able to select all listings that have multiple features (for example a listing that has both "Dishwasher" AND "Garage")

After a couple hours of googling and looking through hibernate documentation haven't been able to find a solution that won't give me an error. My guess is that the solution is pretty simple and I am just over-thinking it...anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-09-02 06:18:08

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

<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() />

谢谢乔恩!

I don't believe this to be the most efficient way to do this, however, it does produce the result I want

<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") />

This will give me only listings that have all the features I am looking for but, it does a LOT of joining and looking at the hibernate SQL log is producing:

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

It just seems like there must be a more efficient way to do this in HQL


Jon Messer on the cf-orm-dev mailing list gave me what I believe is the most correct solution to this question posting it here for everyone:

"As far as I know ORMExecuteQuery doesn't handle list parameters, so if you wanted to param them and return objects you'd have to do something like

<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() />

"

Thanks Jon!

自我难过 2024-09-02 06:18:08

这应该可行:

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

如果您想使用绑定参数,您将必须做一些额外的工作,因为 hibernate 这次不喜欢 ColdFusion 列表和/或数组作为绑定参数。您可以在此处找到一些额外信息

This should work:

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

If you want to go with bound parameters you will have to do some extra work, because hibernate doesn't like ColdFusion Lists and/or Arrays as bound parameters this time. You can find some extra info here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文