HQL:在对象上使用 elements()
假设我有一个包含两个表的数据库:班级和学生。每个表都包含有关相关主题的元数据。例如,班级包含字段“classid”、“name”和“room_number”,学生包含字段“name”、“classid”、“age”和“height”(假设一名学生只能在一个班级中)。
如果我想编写一个 HQL 查询来获取包含姓名为“Joe”、“Bob”和“Fred”的学生的所有班级,该怎么办?如果学生只包含“classid”和“name”,我想我可以在 HQL 中编写以下内容。
from Classes as class where
'Joe' in elements(class.students) and
'Bob' in elements(class.students) and
'Fred' in elements(class.students)
然而,学生是本案中的对象。我需要显式执行连接吗?
from Classes as class
join class.students as s1
join class.students as s2
join class.students as s2
where
s1.name = 'Joe'
s2.name = 'Bob'
s3.name = 'Fred'
我想在 elements 中使用 'Fred' 也可以执行这些连接,但编写起来更加紧凑!或者有更好的方法来完成这个任务吗?
say I have a database with two tables: classes and students. Each table contains metadata about the relevant topic. For example, classes contains fields 'classid', 'name' and 'room_number' and students contains fields 'name', 'classid', 'age' and 'height' (say a student can only be in one class).
What if I want to write an HQL query to get me all classes that include students with names 'Joe', 'Bob', and 'Fred'? If students only contained 'classid' and 'name' I think I could write the following in HQL.
from Classes as class where
'Joe' in elements(class.students) and
'Bob' in elements(class.students) and
'Fred' in elements(class.students)
However, students is an object in this case. Do I need to perform the joins explicitly?
from Classes as class
join class.students as s1
join class.students as s2
join class.students as s2
where
s1.name = 'Joe'
s2.name = 'Bob'
s3.name = 'Fred'
I imagine that using 'Fred' in elements
also performs these joins, but it is much more compact to write! Or is there a better way of doing this altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用子选择:
Use a sub-select: