CF Hibernate仅接受具有命名参数的IN(x)数组,而不接受位置参数

时间:2014-02-17 13:33:29

标签: hibernate coldfusion hql

此示例代码演示了此问题:

var Names = ['Jon','Anna'];

var Params.Pos1   = [ Names[1] ];
var Params.Pos2   = [ Names[1] , Names[2] ];
var Params.Pos3   = [ Names ];
var Params.Named1 = { name:Names[1] };
var Params.Named2 = { name1:Names[1] , name2:Names[2] };
var Params.Named3 = { names:Names };

var BaseHql = "SELECT COUNT(*) FROM Person WHERE ";
var Queries.Pos1   = BaseHql & "FirstName IN (?)";
var Queries.Pos2   = BaseHql & "FirstName IN (?,?)";
var Queries.Pos3   = BaseHql & "FirstName IN (?)";
var Queries.Named1 = BaseHql & "FirstName IN (:name)";
var Queries.Named2 = BaseHql & "FirstName IN (:name1,:name2)";
var Queries.Named3 = BaseHql & "FirstName IN (:names)";

var Results = {};

for ( var Item in Queries )
{
    try
    {
        Results[Item] = OrmExecuteQuery( Queries[Item] , Params[Item] );
    }
    catch (any cfcatch)
    {
        Results[Item] = "[#cfcatch.message#|#cfcatch.detail#]";
    }
}

六种情景中有五种有效。

Queries.Pos3不起作用,尽管与Queries.Named3完全相同。

它返回此错误消息和详细信息:

  

无法将复杂对象类型转换为简单值。

     

表达式已请求变量或中间表达式结果作为简单值。但是,结果无法转换为简单值。简单值是字符串,数字,布尔值和日期/时间值。查询,数组和COM对象是复杂值的示例。

错误的最可能原因是您尝试将复杂值用作简单值。例如,您尝试在cfif标记中使用查询变量。

堆栈跟踪是:

coldfusion.runtime.CfJspPage$ComplexObjectException: Complex object types cannot be converted to simple values.
    at coldfusion.runtime.Cast._String(Cast.java:1036)
    at coldfusion.runtime.Cast._cast(Cast.java:192)
    at coldfusion.orm.ORMUtils.getTypedValue(ORMUtils.java:359)
    at coldfusion.orm.hibernate.HibernatePersistenceManager.setQueryParams(HibernatePersistenceManager.java:844)
    at coldfusion.orm.hibernate.HibernatePersistenceManager._executeHQL(HibernatePersistenceManager.java:781)
    at coldfusion.orm.hibernate.HibernatePersistenceManager.executeHQL(HibernatePersistenceManager.java:753)
    at coldfusion.orm.hibernate.HibernatePersistenceManager.executeQuery(HibernatePersistenceManager.java:615)
    at coldfusion.orm.ORMUtils._executeQuery(ORMUtils.java:344)
    at coldfusion.orm.ORMUtils._executeQuery(ORMUtils.java:311)
    at coldfusion.orm.ORMUtils.executeQuery(ORMUtils.java:303)
    at coldfusion.runtime.CFPage.ORMExecuteQuery(CFPage.java:8933)

上面的代码是否有问题,或者这是CF或Hibernate的错误/限制?

1 个答案:

答案 0 :(得分:2)

Hibernate似乎使用setParameterList将数组设置为单个参数。

relevant javadoc page显示four versions of setParameterList,但它们都只接受命名参数 - 与setParameter方法不同,后者的版本包含名称或位置参数。

因此,似乎是对Hibernate的任意限制,你不能将数组作为位置参数传递。

即。这段代码不起作用:

OrmExecuteQuery( "FROM Person WHERE FirstName IN (?)" , [ArrayOfNames] );

查询需要切换到命名参数,或者位置参数需要展平(例如,可能使用?#RepeatString(',?',ArrayLen()-1)#或类似参数。)