Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 集合属性的HQL查询_Hibernate_Hql_Hbm - Fatal编程技术网

Hibernate 集合属性的HQL查询

Hibernate 集合属性的HQL查询,hibernate,hql,hbm,Hibernate,Hql,Hbm,假设我有以下HBM映射: <class name="Student" table="student"> <set name="classes" table="student_classes" cascade="none"> <key column="studentId" /> <many-to-many column="classId" class="org.myCompany.myClass" />

假设我有以下HBM映射:

<class name="Student" table="student">
    <set name="classes" table="student_classes" cascade="none">
        <key column="studentId" />
        <many-to-many column="classId" class="org.myCompany.myClass" />
    </set>
</class>
但是,hibernate会引发以下异常:

ERROR [service-j2ee-4] PARSER.reportError(33) |  Invalid path: 'classes.className'
ERROR [service-j2ee-4] PARSER.reportError(33) | <AST>:0:0: unexpected end of subtree
ERROR [service-j2ee-4] PARSER.reportError(33) |  left-hand operand of a binary operator was null
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'classes.className' [select count(*) from Student where classes.className = :myClassName  and  1=1]
下面是我的理由:
classes
是一个集合,因此没有
className
属性。使用联接遍历关系后,myClass实体上会有一个别名,该别名具有
className
属性

注意:Java中的类应始终以大写字母开头。将
myClass
重命名为
myClass

是否可以运行基于集合属性返回结果的hibernatequery

对!

在上面的例子中

我们确定
org.myCompany.myClass
ha
className
属性吗? 如果是的话,试试这样的
从学生的内部联接类c中选择count(*),其中c.className=:myClassName

实际上HQL没有良好的内部联接支持(请参阅)。我的集合包含myClass类型的对象,它的属性为className。我想基于className属性(而不是集合本身的属性)进行查询。是的,它有很好的支持。你看过你发来的邮件的答案了吗。您不必像在SQL中那样指定join的on子句,因为映射已经定义了表如何链接在一起。你试过我的问题了吗?Read我刚刚尝试了你的查询,结果出现了错误:“路径应为join!无效路径:clazz.className”@David:你接受了我的答案。为什么会出现“Path expected for join”错误?当我更仔细地查看实际查询时,发现className之前缺少了“c.”别名(这导致了错误)。但是,我现在从查询中得到0个结果(而不是预期的结果数)。我得到的结果为零,因为我需要向查询中添加类似“%string%”的内容
select count(*) from Student where classes.className = :myClassName
ERROR [service-j2ee-4] PARSER.reportError(33) |  Invalid path: 'classes.className'
ERROR [service-j2ee-4] PARSER.reportError(33) | <AST>:0:0: unexpected end of subtree
ERROR [service-j2ee-4] PARSER.reportError(33) |  left-hand operand of a binary operator was null
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'classes.className' [select count(*) from Student where classes.className = :myClassName  and  1=1]
select count(*) as col_0_0_ 
from student student0_, student_classes student1_, classes student2_

where student0_.studentId=student1_.studentId and student1_.classId=student2_.classId and student2_.className LIKE 'algebra' and 1=1;
select count(s.id) from Student s
inner join s.classes clazz
where clazz.className = :myClassName