HibernateSystemException:无法从Grails中的Class [java.lang.Long]解析实体名称

时间:2015-03-31 08:04:44

标签: hibernate grails hql gorm criteria

我尝试使用Grails 2.4.3中的Model.executeQuery使用Hibernate 3插件执行以下查询,其中Model是我的扩展CatalogueElement的域类(tablePerHierarchy false } set):

select count(m)
from Model as m
where m.status = :status
    and m not in (select r1.destination from Relationship r1 where r1.relationshipType = :type)
    and m in (select r2.destination from Relationship r2 where r2.relationshipType = :classificationType and r2.source in :classifications)

当我执行它时,我遇到以下异常:

org.springframework.orm.hibernate3.HibernateSystemException: Unable to resolve entity name from Class [java.lang.Long] expected instance/subclass of [org.modelcatalogue.core.CatalogueElement]; nested exception is org.hibernate.HibernateException: Unable to resolve entity name from Class [java.lang.Long] expected instance/subclass of [org.modelcatalogue.core.CatalogueElement]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:708)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:414)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:416)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:348)

域类看起来类似于:

class CatalogueElement { String name }
class Model extends CatalogueElement { ... }
class Relationship {
    CatalogueElement source
    CatalogueElement destination
}

问题显然是由子查询语句引起的,好像我跳过它们一切正常。实际上,当使用带有DetachedCriteria的子查询时,我遇到了类似的错误。有人有解决方案吗?我不想首先获取子查询并将结果传递给参数,因为它可能会在数千条记录中结束。

1 个答案:

答案 0 :(得分:0)

实际上问题出现在:classifications命名参数中,该参数设置为Long ID,而不是CatalogueElement个实体。正确的查询是

select count(m)
from Model as m
where m.status = :status
    and m not in (select r1.destination from Relationship r1 where r1.relationshipType = :type)
    and m in (select r2.destination from Relationship r2 where r2.relationshipType = :classificationType and r2.source.id in :classifications)