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

标签 hibernate orm coldfusion hql coldfusion-9

我有以下设置:

list .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";    
} 

功能.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 文档后,未能找到不会给我错误的解决方案。我的猜测是解决方案非常简单,我只是想太多了......有人有什么建议吗?

最佳答案

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

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

"

谢谢乔恩!

关于hibernate - 用于多对多和多个条件的 CF9 HQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521016/

相关文章:

nhibernate - 如何使用 NHibernate 联合子类映射抽象属性?

php - 奇怪的学说 EntityNotFoundException

java - Hibernate更新有时不起作用

hibernate - 在 bootstrap 中拉取域映射并修改 Grails

java - "Errors in named queries: User.findByUserNameAndPassword "- 无法解决

java - 返回 boolean 值而不是列表

java - Hibernate 在 TextFullSearch 上搜索 "join"

mysql - 在 MySQL 中使用 OR 和 HAVING 与使用 AND

coldfusion - 在 Coldfusion 中使用来自动态命名变量的列名

coldfusion - 如何使用cfimage将图像保存为渐进式jpg?